IO - 常用类
IO - 常用类
IO流定义
狭义流方向定义
这里把硬盘理解为内存之外的源即可
input 输入流(程序读入数据): 硬盘--->内存
output 输出流(程序写到文件): 内存--->硬盘
流分类
-
按数据单位:字节流(1Byte = 8bit)、字符流(字节长度取决于编码方式)
-
按数据流向:输入流、输出流
-
按流的角色:字节流、处理流(包装流)
(抽象基类) | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
常用类
FileInputStream
该流用于从文件读取数据
文本文件推荐使用字符流, 不推荐使用字节流
- 测试文件
D:\\file.txt
hello, world!
hello, java.
- FileInputStream 构造器// throws FileNotFoundException
public FileInputStream(String name) {}
public FileInputStream(File file) {}
- 逐个字节
public int read() throws IOException
String filePath = "D:\\file.txt";
int read;
try {
FileInputStream fileInputStream = new FileInputStream(filePath);
while ((read = fileInputStream.read()) != -1) {// 结尾(int) = -1
System.out.print((char)read);
}
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
- 字节数组
public int read(byte b[]) throws IOException
String filePath = "D:\\file.txt";
byte[] buffer = new byte[8];
int l = 0;
try {
FileInputStream fileInputStream = new FileInputStream(filePath);
while ( (l = fileInputStream.read(buffer)) != -1) {
System.out.print(new String(buffer, 0, l));
}
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream
用来创建一个文件并向文件中写数据
-
构造器// throws FileNotFoundException
public FileOutputStream(String name) {} public FileOutputStream(String name, boolean append) {} public FileOutputStream(File file) {} public FileOutputStream(File file, boolean append) {}
-
常用写方法// throws IOException
private native void write(int b, boolean append) ; public abstract void write(int b) ; public void write(byte b[]) ; public void write(byte b[], int off, int len) ;// off 从1开始
-
测试用例
String filePath = "D:\\file_out.txt"; try { FileOutputStream fileOutputStream = new FileOutputStream(filePath); fileOutputStream.write('#'); fileOutputStream.write("\nhello, outputStream\n".getBytes(StandardCharsets.UTF_8)); fileOutputStream.write('#'); fileOutputStream.write("\npartFind\n".getBytes(StandardCharsets.UTF_8), 5, 4); fileOutputStream.close(); /* 上述的操作是覆盖操作, 即先清空文件(如果存在) 后执行各创建操作 # hello, outputStream #Find */ fileOutputStream = new FileOutputStream(filePath, true);// 追加方式 fileOutputStream.write("\nappend\n".getBytes()); fileOutputStream.close(); /* 这里执行了追加方式的写入操作 # hello, outputStream #Find append */ } catch (IOException e) { e.printStackTrace(); }
-
综合案例
读源文件
D:\pic.png
486字节写到目标文件
D:\pic_new.png
保持字节一致String source = "D:\\pic.png"; String target = "D:\\pic_new.png"; try { FileInputStream fileInputStream = new FileInputStream(source); FileOutputStream fileOutputStream = new FileOutputStream(target, true) byte[] buffer = new byte[8]; // 长度随便写的 int l = 0; while ((l = fileInputStream.read(buffer)) != -1) { fileOutputStream.write(buffer, 0, l); // 如果不处理空字节, 则目标文件的字节数为 8 的倍数 -> 488 字节 } fileInputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); }
FileReader
-
构造器 // throws FileNotFoundException
public FileReader(String fileName) {} public FileReader(File file) {}
-
常用读取方法 // throws IOException
public int read() public int read(char cbuf[]) {}
-
测试文件
D:\\file.txt
hello, world! hello, java.
-
案例
String filePath = "D:\\file.txt"; char[] buffer = new char[8]; int l = 0; try { FileReader fileReader = new FileReader(filePath); StringBuilder sb = new StringBuilder(); while ((l = fileReader.read(buffer)) != -1) { sb.append(buffer, 0, l); } fileReader.close(); System.out.println(sb.toString()); } catch (IOException e) { e.printStackTrace(); }
FileWriter
注意,使用 FileWriter 时,必须要关闭(close)或者刷新(flush),否则写入不到指定文件
-
构造器 // throws IOException
public FileWriter(String fileName) {} public FileWriter(String fileName, boolean append) {} public FileWriter(File file) {} public FileWriter(File file, boolean append) {}
-
常用方法 // throws IOException
public void write(int c) {} public void write(char cbuf[], int off, int len) {} public void write(String str, int off, int len) {} public void write(char cbuf[]) {} public void write(String str) {}
-
案例
String targetPath = "D:\\out.txt"; String content = "山远天高烟水寒,相思枫叶丹。"; try { FileWriter fileWriter = new FileWriter(targe fileWriter.write(content); fileWriter.flush();// 不关闭保存 /* 山远天高烟水寒,相思枫叶丹。 */ fileWriter = new FileWriter(targetPath, true fileWriter.write("\n松花酿酒,春水煎茶。"); fileWriter.close(); // 等价于保存并关闭 /* 山远天高烟水寒,相思枫叶丹。 松花酿酒,春水煎茶。 */ } catch (IOException e) { e.printStackTrace(); }