一、流的分类:
- 按照数据流向的不同:输入流 输出流
- 按照处理数据的单位的不同:字节流 字符流(处理的文本文件)
- 按照角色的不同:节点流(直接作用于文件的) 处理流
二、IO的体系
- 抽象基类 节点流(文件流) 缓冲流(处理流的一种)
- InputStream FileInputStream BufferedInputStream
- OutputStream FileOutputStream BufferedOutputStream
- Reader FileReader BufferedReader
- Writer FileWriter BufferedWriter
三、实例
基类InputStream、OutputStream 字节流既可以用来处理媒体数据也可以用来处理文本数据
1. FileInputStream和FileOutputStream
package com.yyx.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileInputStreamOutputStreamDemo { public static void main(String[] args) { testFileInputStreamOne(); System.out.println("================="); testFileInputStreamTwo(); System.out.println("================="); testFileOutputStreamOne(); System.out.println("================="); testInputStreamOutputStreamOne(); System.out.println("================="); testInputStreamOutputStreamTwo(); } /** * 读取文本文件 */ public static void testFileInputStreamOne() { String pathname = "F:" + File.separator + "source.txt"; File file = new File(pathname); FileInputStream fis = null; try { fis = new FileInputStream(file); // 读取到的数据要写入的数组 byte[] b = new byte[5]; // 每次读入到byte中的字节的长度 int len; while ((len = fis.read(b)) != -1) { String str = new String(b, 0, len); System.out.print(str); } System.out.println(); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 读取文本文件 */ public static void testFileInputStreamTwo() { String pathname = "F:" + File.separator + "source.txt"; File file = new File(pathname); FileInputStream fis = null; try { fis = new FileInputStream(file); int b; /* * read():读取文件的一个字节。当执行到文件结尾时,返回-1 此方法如文件内容有中文名出现乱码 */ while ((b = fis.read()) != -1) { System.out.print((char) b); } System.out.println(); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 写入文本文件 */ public static void testFileOutputStreamOne() { String pathname = "F:" + File.separator + "target.txt"; File file = new File(pathname); FileOutputStream fos = null; try { fos = new FileOutputStream(file); String str = "*中国--China*"; fos.write(str.getBytes()); System.out.println("写入成功"); fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 复制文本文件 */ public static void testInputStreamOutputStreamOne() { // 此处两个文件都已存在 String sourcePathName = "F:" + File.separator + "source.txt"; String targetPathName = "F:" + File.separator + "target.txt"; File sourceFile = new File(sourcePathName); File targetFile = new File(targetPathName); FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(targetFile); // 3.实现文件的复制 byte[] b = new byte[1024]; int len; while ((len = fileInputStream.read(b)) != -1) { fileOutputStream.write(b, 0, len); } fileOutputStream.flush(); System.out.println("复制文本成功"); } catch (Exception e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 复制图片文件 */ public static void testInputStreamOutputStreamTwo() { // 此处两个文件都已存在 String sourcePathName = "F:" + File.separator + "source.jpg"; String targetPathName = "F:" + File.separator + "target.jpg"; File sourceFile = new File(sourcePathName); File targetFile = new File(targetPathName); FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(targetFile); // 3.实现文件的复制 byte[] b = new byte[1024]; int len; while ((len = fileInputStream.read(b)) != -1) { fileOutputStream.write(b, 0, len); } System.out.println("复制图片成功"); fileOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
2. BufferedInputStream和BufferedOutputStream
package com.yyx.test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileInputStreamOutputStreamDemo { public static void main(String[] args) { // 此处两个文件都已存在 String sourcePathName = "F:" + File.separator + "source.jpg"; String targetPathName = "F:" + File.separator + "target.jpg"; File sourceFile = new File(sourcePathName); File targetFile = new File(targetPathName); FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; BufferedInputStream bufferedInputStream = null; BufferedOutputStream bufferedOutputStream = null; try { fileInputStream = new FileInputStream(sourceFile); fileOutputStream = new FileOutputStream(targetFile); bufferedInputStream = new BufferedInputStream(fileInputStream); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); // 3.实现文件的复制 byte[] b = new byte[1024]; int len; while ((len = bufferedInputStream.read(b)) != -1) { bufferedOutputStream.write(b, 0, len); } System.out.println("复制图片成功"); bufferedOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedInputStream != null) { try { bufferedInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedOutputStream != null) { try { bufferedOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
基类Reader、Writer 字符流只用来处理文本数据
1. FileReader和FileWriter
package com.yyx.test; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileReaderWriterDemo { public static void main(String[] args) { testReaderOne(); System.out.println("================="); testReaderTwo(); System.out.println("================="); testWriter(); System.out.println("================="); testReaderWriter(); } /** * 推荐:使用字节组读取 */ public static void testReaderOne() { // 方法中文件已经存在 String pathname = "F:" + File.separator + "source.txt"; File file = new File(pathname); FileReader fr = null; try { fr = new FileReader(file); char[] buf = new char[6]; int num = 0; while ((num = fr.read(buf)) != -1) { String str = new String(buf, 0, num); System.out.print(str); } System.out.println(); } catch (Exception e) { e.printStackTrace(); } } /** * 使用单个字母读取 */ public static void testReaderTwo() { // 方法中文件已经存在 String pathname = "F:" + File.separator + "source.txt"; File file = new File(pathname); FileReader fr = null; try { fr = new FileReader(file); int ch; while ((ch = fr.read()) != -1) { System.out.print((char) ch); } System.out.println(); } catch (Exception e) { e.printStackTrace(); } } public static void testWriter() { String pathname = "F:" + File.separator + "target.txt"; File file = new File(pathname); FileWriter fw = null; try { fw = new FileWriter(file); String str = "*中国--China*"; fw.write(str); System.out.println("写入成功"); /* * flush和close的区别:flush刷新后可以继续输入,close刷新后不能继续输入 */ fw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fw != null) { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 复制文件 */ public static void testReaderWriter() { // 此处两个文件都已存在 String sourcePathName = "F:" + File.separator + "source.txt"; String targetPathName = "F:" + File.separator + "target.txt"; File sourceFile=new File(sourcePathName); File targetFile=new File(targetPathName); FileReader fileReader = null; FileWriter fileWriter = null; try { fileReader = new FileReader(sourceFile); // true表示追加写入,默认是false fileWriter = new FileWriter(targetFile, true); char[] buf = new char[1024]; // 将文件读取到buf数组中 int num = 0; while ((num = fileReader.read(buf)) != -1) { fileWriter.write(new String(buf, 0, num)); } System.out.println("复制成功"); fileWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (fileWriter != null) { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
2. BufferedReader和BufferedWriter
- BufferedWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入
- BufferedReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
package com.yyx.test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileReaderWriterDemo { public static void main(String[] args) { // 此处两个文件都已存在 String sourcePathName = "F:" + File.separator + "source.txt"; String targetPathName = "F:" + File.separator + "target.txt"; File sourceFile=new File(sourcePathName); File targetFile=new File(targetPathName); FileReader fileReader = null; FileWriter fileWriter = null; BufferedReader bufferedReader = null; BufferedWriter bufferedWriter = null; try { fileReader = new FileReader(sourceFile); // true表示追加写入,默认是false fileWriter = new FileWriter(targetFile, true); bufferedReader = new BufferedReader(fileReader); bufferedWriter = new BufferedWriter(fileWriter); char[] buf = new char[1024]; // 将文件读取到buf数组中 int num = 0; while ((num = bufferedReader.read(buf)) != -1) { bufferedWriter.write(new String(buf, 0, num)); } System.out.println("复制成功"); bufferedWriter.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
原文地址:https://www.cnblogs.com/xianya/p/9191217.html
时间: 2024-10-07 21:38:33