四类节点流
一.首先,涉及到的流函数操作都是遵循以下四个步骤
1.File类的实例化
2.File流的实例化
3.读入/写入的操作
4.流资源的关闭
即整体思维框架是一样的,不同的流只是在一些细节性的语法上做修改
二.流的操作涉及到异常处理
有关流的操作会涉及到FileNotFoundException和IOException这两类异常
流操作的异常主要发生在两个位置:1.定义并初始化IO流的时候 2.在关闭清空自己构造出的流时
对于这些异常,我们一般采用try-catch-finally来进行处理
三.IO流中的读写函数:
1.读入流函数read():(1).无参:返回读入的一个字符。如果达到文件末尾,返回-1
(2).有特定参数:如是一个字符数组,则每次读入返回和字符数组长度相同的字符串,直到全部读完为止,返回的参数为读到的字符串的长度
2.写出流函数write(Object obj,bool type):(1).将obj内容写入到对应的字符串中,obj常为int型或是字符/字节数组型.
(2).type中false表示写入的方式是将原文件内容替换,true表示写入的方式是将原文件的内容进行增添,type的默认值为false
FileReader和FileWriter:处理的是字符型数据,主要处理的文件是文本文件(.txt,.java,.c,.cpp)
FileReader和FileWriter的代码实例:
1 //综合应用:根据输入和输出流实现文件的复制 2 public static void FileCopy() 3 { 4 FileReader fr = null; 5 FileWriter fw = null; 6 try { 7 File file1 = new File("hello.txt"); 8 File file2 = new File("hi.txt"); 9 10 fr = new FileReader(file1); 11 fw = new FileWriter(file2); 12 13 int data = 0; 14 while ((data = fr.read()) != -1) //没有读到数据时,data的值为-1 15 { 16 fw.write(data); 17 } 18 } catch (FileNotFoundException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } catch (IOException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } finally { 25 if (fr != null) 26 try { 27 fr.close(); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 if (fw != null) 33 try { 34 fw.close(); 35 } catch (IOException e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 } 40 }
对文件中的字符进行计数
1 public static void countFileTest(String path) throws IOException 2 { 3 Map<Character,Integer> map = new HashMap();//以Character类作为键值 4 File file = new File(path); 5 FileReader fr = new FileReader(file); 6 int data; 7 while ((data = fr.read()) != -1) 8 { 9 char ch = (char) data;//因为计的是字符,所以要将int型data强转为char类型 10 if (map.get(ch) == null) 11 { 12 map.put(ch,1); 13 } 14 else 15 { 16 map.put(ch,map.get(ch) + 1); 17 } 18 } 19 Set<Map.Entry<Character,Integer> > entrySet = map.entrySet();//构造得到Map中的entry数组(为了能够遍历到所有的键值对),注意entrySet集合的定义方法 20 for (Map.Entry<Character, Integer> entry : entrySet)//对键值对进行遍历 21 { 22 System.out.println(entry.getKey() + ":" + entry.getValue()); 23 } 24 fr.close(); 25 }
FileInputStream和FileOutputStream:主要处理的是字节流,处理非文本文件(.png,.mp3,.mp4,.avi,.doc,.ppt)
FileInputStream和FileOutputStream的代码实例:
1 //使用字节流处理文本文件可能出现乱码(如在) 2 public static void testFileInputStream() 3 { 4 //2.造流 5 FileInputStream fis = null; 6 try { 7 //1.造文件 8 File file = new File("hello.txt"); 9 fis = new FileInputStream(file); 10 //3.文件数据读入 11 byte[] charBuff = new byte[5]; 12 int len; 13 while ((len = fis.read(charBuff)) != -1) 14 { 15 String str = new String(charBuff,0,len); 16 System.out.println(str); 17 } 18 } catch (Exception e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } finally { 22 if (fis != null) 23 try { 24 fis.close(); 25 } catch (IOException e) { 26 // TODO Auto-generated catch block 27 e.printStackTrace(); 28 } 29 } 30 }
1 public static void testFileInputOutputStream() 2 { 3 FileInputStream fis = null; 4 FileOutputStream fos = null; 5 try { 6 File srcFile = new File("1.jpg"); 7 File desFile = new File("2.jpg"); 8 9 fis = new FileInputStream("srcFile"); 10 fos = new FileOutputStream("desFile"); 11 12 byte[] buffer = new byte[5]; 13 int len; 14 while ((len = fis.read(buffer)) != -1) 15 { 16 fos.write(buffer,0,len); 17 } 18 } catch (FileNotFoundException e) { 19 // TODO Auto-generated catch block 20 e.printStackTrace(); 21 } catch (IOException e) { 22 // TODO Auto-generated catch block 23 e.printStackTrace(); 24 } finally { 25 if (fis != null) 26 try { 27 fis.close(); 28 } catch (IOException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 if (fos != null) 33 try { 34 fos.close(); 35 } catch (IOException e) { 36 // TODO Auto-generated catch block 37 e.printStackTrace(); 38 } 39 } 40 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)