package cn.edu.fhj.day009.FileDemo; import java.io.File; import java.io.IOException; public class FileDemo { public static void main(String[] args) throws IOException { // 将路径描述成File对象 // File file = new File("d:/java_fd_test/fileDemo.txt"); File file = new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm"); boolean exists = file.exists(); // 如果路径所表示的文件或者文件夹存在,则返回true System.out.println(exists); // 判断该file是文件夹还是文件 boolean directory = file.isDirectory(); System.out.println(directory); // true boolean ifFile = file.isFile(); System.out.println(ifFile); // false // 获取文件的绝对路径 String absolutePath = file.getAbsolutePath(); System.out.println(absolutePath); // 可以获取文件名或文件夹名 String name2 = file.getName(); System.out.println(name2); File file2 = new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt"); ifFile = file2.isFile(); // true System.out.println(ifFile); // 获取文件名 String name = file2.getName(); System.out.println(name); // 获取上一级目录的file对象 File parentFile = file2.getParentFile(); System.out.println(parentFile.getAbsolutePath()); // 获取上一级目录的路径字符串 String parent = file2.getParent(); System.out.println(parent); // 获取文件长度 字节(8个bit-- 二进制位) long length = file2.length(); System.out.println(length); System.out.println("------------------------"); // 获取指定目录下的子节点的名称字符串 String[] list = file.list(); for (String s : list) { System.out.println(s); } System.out.println("------------------------"); // 获取指定目录下的子节点的File描述对象 File[] listFiles = file.listFiles(); for (File f : listFiles) { System.out.println(f.getAbsolutePath()); } System.out.println("------------------------"); // 创建一个文件夹 File f = new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz"); // boolean mkdir = f.mkdir(); // 不能创建多级目录 // System.out.println(mkdir); // boolean mkdirs = f.mkdirs(); // 可以创建多级目录 // System.out.println(mkdirs); // // // 创建文件 File file3 = new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz/cls.txt"); boolean createNewFile = file3.createNewFile(); System.out.println(createNewFile); // 重命名文件:其实可以把路径都给改了 file3.renameTo(new File( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/xx/yy/zz/cls001.txt")); // 删除文件 boolean delete = file3.delete(); System.out.println(delete); } }
FileOutputStreamDemo文件的写
package cn.edu.fhj.day009.FileDemo; import java.io.FileOutputStream; public class FileOutputStreamDemo { public static void main(String[] args) throws Exception { // 覆盖的方式写数据 FileOutputStream fos = new FileOutputStream( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt"); String s = "a你好"; byte[] bytes = s.getBytes(); fos.write(bytes); // 将字符串按指定编码集编码--》将信息转成二进制数 fos.write(bytes); // 这样写入的数据,会将文件中的原数据覆盖 // 追加的方式写数据:如果要往一个文件中追加数据,则在FileOutputStream的构造参数中多传一个true FileOutputStream fos2 = new FileOutputStream( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt", true); fos2.write(",sb".getBytes("UTF-8")); fos2.close(); /** * 第一句和后两句话写到文件中的数据完全相同 */ fos.write("我用一生一世为你祈祷".getBytes()); // .getBytes()编码的过程 fos.write((byte) 49); fos.write((byte) 51); /** * 这两句话写到文件中的数据完全相同 */ // fos.write((byte)13); // fos.write("\r".getBytes()); fos.close(); } }
FileInputStreamDemo的读
package cn.edu.fhj.day009.FileDemo; import java.io.FileInputStream; import java.io.InputStream; public class FileInputStreamDemo { public static void main(String[] args) throws Exception { // 要读文件,首先要构造一个FileInputStream对象 InputStream fis = new FileInputStream( "F:/up_mouth_10/code_data/Data_Structure_And_Algorithm/Demo.txt"); /** * 把数从文件中读取出来 如何读取字符 */ // FileInputStream是一种字节流,是按照一个一个字节去文件中取数据的 // 手动一个字节一个字节地读取 /* * int read = fis.read(); * * System.out.println(read); * * read = fis.read(); System.out.println(read); */ /** * 利用fis读到文件末尾后会返回-1的特性,来用循环进行读取 */ int read = 0; /* * while((read=fis.read())!=-1) { System.out.println(read); } */ System.out.println("-------------------"); /** * 如果我要读出数据(文本文件中的数据其实就是字符) 过程是:还是先读数,然后按照码表,将这个数转成字符 * */ /* * read = 0; while((read=fis.read())!=-1) { // * char就代表一个英文字符,而且使用的是ascII码表规则 char c = (char)read; * System.out.println(c); } */ /** * 一次读取多个字节然后转成某种数据类型 read(buf)方法,一次读取buf长度个字节数据,并且读到的数据直接填入了buf数组中 */ /* * byte[] buf = new byte[8]; int num = fis.read(buf); // 返回的是真实读到的字节数量 * String string = new String(buf,2,5); // 利用二进制的byte数组来转成字符串 * System.out.println(string); */ /** * 用while循环来反复读取 */ int num = 0; byte[] buf = new byte[8]; while ((num = fis.read(buf)) != -1) { System.out.println(new String(buf, 0, num)); } // 关流 fis.close(); } }