文件流
底层数据流转示意图:

文件流:
1 import java.io.File; 2 import java.io.IOException; 3 4 public class DemoClass4IO2 { 5 public static void main(String[] args) throws IOException { 6 //TODO Java IO - 文件流 7 8 /* 9 * 1. File 文件类(指:文件 或者 文件夹),属于java.io.File 10 * */ 11 //String filePath = "D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io.txt"; 12 String filePath = "D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data"; 13 File file = new File(filePath); 14 System.out.println(file); //D:\1_code\myjava\mystudy\JavaCodeProject\data\io.txt 15 16 //1.1 文件对象的操作 17 //TODO 判断是否是文件,因为File语法:有文件 还有文件夹的意思 18 System.out.println(file.isFile()); //true 19 20 //TODO 判断是否是文件夹 21 System.out.println(file.isDirectory()); //false 22 23 //TODO 判断文件是否存在关联 24 System.out.println(file.exists()); //true 25 26 //TODO 创建文件目录 27 //file.mkdir(); 28 29 //TODO 创建文件 30 //file.createNewFile(); 31 32 if(file.exists()) { 33 if(file.isFile()){ 34 //TODO 存在文件关联 35 System.out.println("文件存在,存在关联关系"); 36 System.out.println("文件名:"+file.getName()); //文件名:io.txt 37 System.out.println("文件长度:"+file.length()); //文件长度:11 38 System.out.println("文件最后修改时间:"+file.lastModified()); //文件最后修改时间:1761026131762 39 System.out.println("文件绝对路径:"+file.getAbsolutePath()); //文件绝对路径:D:\1_code\myjava\mystudy\JavaCodeProject\data\io.txt 40 41 }else if(file.isDirectory()){ 42 //TODO 存在文件夹关联 43 System.out.println("文件夹存在,存在关联关系"); 44 System.out.println("文件名:"+file.getName()); //文件名:data 45 System.out.println("文件最后修改时间:"+file.lastModified()); //文件最后修改时间:1761027185973 46 System.out.println("文件绝对路径:"+file.getAbsolutePath()); //文件绝对路径:D:\1_code\myjava\mystudy\JavaCodeProject\data 47 48 //文件夹中的文件,返回文件名称集合 49 String[] list = file.list(); 50 for (String s : list) { 51 System.out.println("遍历得到的文件名称集合:"+s); 52 } 53 /* 54 * 输出结果: 55 遍历得到的文件名称集合:io.txt 56 遍历得到的文件名称集合:newdata 57 * */ 58 59 //获取文件对象(也就是问文件绝对路径)集合 60 System.out.println("------------"); 61 File[] files = file.listFiles(); 62 for (File file1 : files) { 63 System.out.println("遍历得到的文件集合:"+file1); 64 } 65 /* 66 * 输出结果: 67 遍历得到的文件集合:D:\1_code\myjava\mystudy\JavaCodeProject\data\io.txt 68 遍历得到的文件集合:D:\1_code\myjava\mystudy\JavaCodeProject\data\newdata 69 * */ 70 } 71 }else{ 72 System.out.println("文件不存在,不存在关联关系"); 73 //TODO 创建文件目录 74 //file.mkdir(); //data目录下多出一个newdata文件夹 75 76 //TODO 创建文件 77 //file.createNewFile(); //kfc 文件 78 } 79 } 80 }
字节流:
文件复制:(打开一次阀门,流转一个数据,效率低)

1 import java.io.*; 2 3 public class DemoClass4IO3 { 4 public static void main(String[] args) { 5 //TODO Java IO - 文件复制 6 File srcFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io.txt"); 7 File destFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io2.txt"); 8 9 //TODO 文件输入流(管道对象) 10 FileInputStream in = null; 11 //TODO 文件输出流(管道对象) 12 FileOutputStream out = null; 13 14 try { 15 in = new FileInputStream(srcFile); 16 out = new FileOutputStream(destFile); 17 18 //TODO 打开输入阀门,流转数据 19 //int data = in.read(); 20 //TODO 打开输出阀门,流转数据 21 //out.write(data); //io2.txt 确实有了,但是里面内容为:h 少于:io.txt 里hello china 的内容 22 23 //TODO 打开输入阀门,流转数据 24 //int data2 = in.read(); 25 //TODO 打开输出阀门,流转数据 26 //out.write(data2); //io2.txt 确实有了,但是里面内容为:he 少于:io.txt 里hello china 的内容 27 28 /* 29 * 总结: 30 * 问题1;代码重复性比较强 31 * 问题2:多读数据的时候,会返回data=-1,在write,会乱码,产生错误数据 32 * 33 * 针对上面,可以使用循环遍历打开输入、输出阀门 34 * */ 35 int data = -1; 36 while((data = in.read()) != -1){ 37 out.write(data); 38 } 39 //输出结果:io2.txt里面是:hello 40 41 }catch (IOException e){ 42 throw new RuntimeException(e); 43 }finally { 44 if(in != null){ 45 try{ 46 in.close(); 47 }catch (IOException e2){ 48 throw new RuntimeException(e2); 49 } 50 } 51 if(out != null){ 52 try{ 53 out.close(); 54 }catch (IOException e2){ 55 throw new RuntimeException(e2); 56 } 57 } 58 } 59 60 } 61 }
字节流:
缓冲流:Buffered(阀门打开2次即可)

1 import java.io.*; 2 3 public class DemoClass4IO4 { 4 public static void main(String[] args) { 5 //TODO Java IO - 文件复制-缓冲区 Buffered 6 File srcFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io.txt"); 7 File destFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io3.txt"); 8 9 //TODO 文件输入流(管道对象) 10 FileInputStream in = null; 11 //TODO 文件输出流(管道对象) 12 FileOutputStream out = null; 13 14 //TODO 缓冲输入流(管道对象) 15 BufferedInputStream buffin = null; 16 //TODO 缓冲输出流(管道对象) 17 BufferedOutputStream buffout = null; 18 19 //TODO 缓冲区(水桶) 20 byte[] bucket = new byte[1024]; 21 22 try { 23 in = new FileInputStream(srcFile); 24 out = new FileOutputStream(destFile); 25 26 buffin = new BufferedInputStream(in); 27 buffout = new BufferedOutputStream(out); 28 29 int data = -1; 30 while((data = buffin.read(bucket)) != -1){ 31 buffout.write(bucket, 0, data); // 0 data 参数决定:有多少数据,读取多少数据 32 } 33 34 }catch (IOException e){ 35 throw new RuntimeException(e); 36 }finally { 37 if(buffin != null){ 38 try{ 39 buffin.close(); 40 }catch (IOException e2){ 41 throw new RuntimeException(e2); 42 } 43 } 44 if(buffout != null){ 45 try{ 46 buffout.close(); 47 }catch (IOException e2){ 48 throw new RuntimeException(e2); 49 } 50 } 51 } 52 } 53 }
字符流:
1. 说明为什么要字符流
1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 6 public class DemoClass4IO5 { 7 public static void main(String[] args) { 8 //TODO Java IO - 字符流 9 File srcFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io.txt"); 10 File destFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io4.txt"); 11 12 //TODO 文件输入流(管道对象) 13 FileInputStream in = null; 14 //TODO 文件输出流(管道对象) 15 FileOutputStream out = null; 16 17 try { 18 in = new FileInputStream(srcFile); 19 out = new FileOutputStream(destFile); 20 21 int data = -1; 22 /* 23 while((data = in.read()) != -1){ 24 //out.write(data); 25 System.out.println(data); 26 *//* 27 * 打印结果:hello 打印出来:ascII 28 * byte -> -128~127 29 * ascII -> 0~127 30 * 31 * 32 104 33 101 34 108 35 108 36 111 37 * 中国 打印出来:Unicode 38 * 用三个字节标识除了ascII以外的字符 39 * 所以一个汉字三个字节,并且都是大于127的 40 * 41 228 42 184 43 173 44 229 45 155 46 189 47 * *//* 48 49 // 结论:遍历的data就是一个一个字节了,,这样就可以针对字节来操作,实现修改数据 50 //但是这样不现实,太麻烦了,所以直接操作字符是最快捷的 51 }*/ 52 53 //字符流的写法,实现修改内容(一般都是新增内容) 54 StringBuilder sb = new StringBuilder(); 55 while((data = in.read()) != -1){ 56 sb.append((char)data); 57 } 58 sb.append(" china"); 59 System.out.println(sb.toString());//hello china 60 61 //可以将字符串转为字节数组,再讲数组中的每一个字节写入文件中 62 //常用的是字符操作,而不是字节,且看下面文章: 63 64 }catch (IOException e){ 65 throw new RuntimeException(e); 66 }finally { 67 if(in != null){ 68 try{ 69 in.close(); 70 }catch (IOException e2){ 71 throw new RuntimeException(e2); 72 } 73 } 74 if(out != null){ 75 try{ 76 out.close(); 77 }catch (IOException e2){ 78 throw new RuntimeException(e2); 79 } 80 } 81 } 82 83 } 84 }
2.字符流的使用
1 import java.io.*; 2 3 public class DemoClass4IO5 { 4 public static void main(String[] args) { 5 //TODO Java IO - 字符流 把多个字节转为字符,来流转,提高效率 6 File srcFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io.txt"); 7 File destFile = new File("D:\\1_code\\myjava\\mystudy\\JavaCodeProject\\data\\io5.txt"); 8 9 //TODO 字符输入流(管道对象) 10 BufferedReader reader = null; 11 //TODO 字符输出流(管道对象) 12 PrintWriter writer = null; 13 14 try { 15 reader = new BufferedReader(new FileReader(srcFile)); 16 writer = new PrintWriter(destFile); 17 18 //读取文件中一行的数据(字符串) 19 String line = null; 20 while((line = reader.readLine()) != null){ 21 System.out.println("读取的是字符串:"+line); 22 writer.println(line); 23 } 24 //刷写数据 25 writer.flush(); //类似:0 参数的作用; buffout.write(bucket, 0, data); // 0 data 参数决定:有多少数据,读取多少数据 26 27 }catch (IOException e){ 28 throw new RuntimeException(e); 29 }finally { 30 if(reader != null){ 31 try{ 32 reader.close(); 33 }catch (IOException e2){ 34 throw new RuntimeException(e2); 35 } 36 } 37 if(writer != null){ 38 writer.close(); 39 } 40 } 41 } 42 }

浙公网安备 33010602011771号