一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Java通过字节流缓冲流转换流与对象流实现文件读写的代码示例

时间:2026-05-27 10:30:02 编辑:袖梨 来源:一聚教程网

Java作为主流的编程语言,其IO流体系为文件操作提供了多样化的解决方案。本文将系统介绍字节流、缓冲流、转换流与对象流的使用方法,帮助开发者掌握文件读写的核心技巧。 一、Java 中有哪些流 二、Java 中流的分类 节点流(从文件内容到流的输入输出)字节流 FileInputStream FileOutputStream字符流 FileReader FileWriter处理流(基于节点流的进一步封装)缓冲流 BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter转换流 DataInputStream DataOutputStream对象流 ObjectInputStream ObjectOutputStream 三、Java 中流如何使用 字节流可以读写文本、图片、音频、视频等各种类型的文件。比如 world、png、mp4 等。字符流只可以读写文本文件比如 txt、java、py、c 等。注意 world excel ppt 不属于文本文件。缓冲流作用:先把数据缓存到内存中,然后批量写入磁盘中,减少跟磁盘交互的次数,可以提高读写文件的效率。如果不使用缓冲流,读一次写一次跟磁盘交互一次,跟磁盘交互次数多,读写效率较低。转换流用于文件内容的编码和解码,文件内容中保存的是二进制数据,解码后,我们才可以看懂。解码,把二进制数据按照指定的字符集转换为字符。编码,把字符按照指定字符集编码为二进制数据。对象流用于把对象序列化为二进制数据,以便持久化到磁盘或使用网络传输。反序列化,把二进制数据反序列化为内存中的 Java 对象。 四、使用字节流读写文件 public void copyFileWithFile(String srcPath, String destPath) { FileInputStream fis = null; FileOutputStream fos = null; try { //1. File srcFile = new File(srcPath); File destFile = new File(destPath); //2. fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); //3. 读写过程 int len; byte[] buffer = new byte[100]; while ((len = fis.read(buffer)) != -1) { fos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { //4. 关闭资源 try { if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); } } } 五、使用缓冲流读写文件 public void copyFileWithBuffered(String srcPath, String destPath) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //1. File srcFile = new File(srcPath); File destFile = new File(destPath); //2. FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(destFile); bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); //3. 读写过程 int len; byte[] buffer = new byte[100]; while ((len = bis.read(buffer)) != -1) { bos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { //4. 关闭资源(1. 需要先关闭缓冲流,再关闭文件流 2. 默认情况下,关闭外层流时,也会自动关闭内部的流) try { if(bos != null) bos.close(); } catch (IOException e) { e.printStackTrace(); } try { if(bis != null) bis.close(); } catch (IOException e) { e.printStackTrace(); } } } 六、字符流只能读写文本文件 public void test() { InputStreamReader isr = null; OutputStreamWriter osw = null; try { isr = new InputStreamReader(new FileInputStream("康师傅的话.txt"),"gbk"); osw = new OutputStreamWriter(new FileOutputStream("C:UsersshkstartDesktop寄语.txt"),"utf-8"); char[] cbuf = new char[1024]; int len; while ((len = isr.read(cbuf)) != -1) { osw.write(cbuf, 0, len); osw.flush(); } System.out.println("文件复制完成"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (isr != null) isr.close(); } catch (IOException e) { e.printStackTrace(); } try { if (osw != null) osw.close(); } catch (IOException e) { e.printStackTrace(); } } } 七、使用转换流进行解码编码 使用 UTF8 解码文件得到文件内容后使用 GBK 编码文件输出到新的文件中 public void test4() { InputStreamReader isr = null; OutputStreamWriter osw = null; try { //1. 造文件 File file1 = new File("dbcp_gbk.txt"); File file2 = new File("dbcp_gbk_to_utf8.txt"); //2. 造流 FileInputStream fis = new FileInputStream(file1); isr = new InputStreamReader(fis,"GBK"); FileOutputStream fos = new FileOutputStream(file2); osw = new OutputStreamWriter(fos,"utf8"); //3. 读写过程 char[] cBuffer = new char[1024]; int len; while((len = isr.read(cBuffer)) != -1){ osw.write(cBuffer,0,len); } System.out.println("操作完成"); } catch (IOException e) { throw new RuntimeException(e); } finally { //4. 关闭资源 try { if(osw != null) osw.close(); } catch (IOException e) { throw new RuntimeException(e); } try { if(isr != null) isr.close(); } catch (IOException e) { throw new RuntimeException(e); } } } 八、使用对象流序列化反序列化

热门栏目