java IO流
- 字节流
1.1. FileInputStream
1.1.1.public FileInputStream(String name) throws FileNotFoundException;//创建文件输入流
- 用来读取本地文件中的数据
- 入参name是文件地址
- 当文件路径不存在会报错java.io.FileNotFoundException
- 当文件不存在时会报错java.io.FileNotFoundException
1.1.2.public void close() throws IOException;//关闭流
1.1.3.public int read() throws IOException;//读取文件内容
- 每次读取一个字节
- 返回读取的数据
- 读到文件尾返回-1
- 读到文件尾后继续读依旧返回-1
FileInputStream使用read()读取文件示例
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class M01 {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("C:\\Users\\gaogu\\Desktop\\iotest.txt");
int b;
while ((b = fileInputStream.read()) != -1) {
System.out.println((char)b);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1.1.4. public int read(byte b[], int off, int len) throws IOException //读取文件内容
- 每次读len个字节的数据放入b[off]到b[off+len]中,不足len个字节时剩多少个读多少个
- 返回本次读到了多少个字节
- 当本次读取到0个字节时返回-1
- public int read(byte b[]) == public int read(byte b[], 0, b.length)
FileInputStream使用read(byte b[], int off, int len)读取文件示例
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class M02 {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("C:\\Users\\gaogu\\Desktop\\iotest.txt");
int b;
byte[] bytes = new byte[4]; //一般是1024的整数倍
while ((b = fileInputStream.read(bytes)) != -1) {
for (int i = 0; i < b; i++) {
System.out.println((char)bytes[i]);
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1.2. FileOutputStream
1.2.1. public FileOutputStream(String name, boolean append) throws FileNotFoundException; //创建文件输出流
- 用来写出数据到本地文件中
- 入参name是文件地址
- 入参append表示是否开启续写,false表示创建文件输出流时会清空文件内容,true则不会清空文件内容
- 当文件路径不存在会报错java.io.FileNotFoundException
- 当文件路径存在但文件不存在时会创建文件
- public FileOutputStream(String name) == public FileOutputStream(String name, false)
1.2.2. public void close() throws IOException; //关闭流
1.2.3. public void write(int b) throws IOException; //写出内容到文件
- 往文件尾部追加数据
- 每次写出一个字节到文件中
- 截取入参b低位一个字节写出到文件中
FileOutputStream使用write(int b)写出数据到文件示例
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class M03 {
public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("C:\\Users\\gaogu\\Desktop\\iotest.txt");
fileOutputStream.write((int)'a');
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1.2.4. public void write(byte b[], int off, int len) throws IOException; //写出内容到文件
- 往文件尾部追加数据
- 每次将b[off]到b[off+len]的数据写出到文件
- public void write(byte b[]) == public void write(byte b[], 0, b.length)
FileOutputStream使用write(byte b[], int off, int len)写出数据到文件示例
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class M04 {
public static void main(String[] args) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("C:\\Users\\gaogu\\Desktop\\iotest.txt");
byte[] bytes = new byte[] {(byte)'a',(byte)'b',(byte)'c',(byte)'d',(byte)'e',(byte)'f',(byte)'g'};
fileOutputStream.write(bytes);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1.3. BufferedInputStream
- 带缓冲区的输入流
1.3.1. public BufferedInputStream(InputStream in, int size); //创建缓冲输入流
- 用来从某处(某处取决于入参in)读取数据
- 入参size是缓冲区大小(单位:字节)
- public BufferedInputStream(InputStream in); == public BufferedInputStream(InputStream in, 8192);
1.3.2. public synchronized int read() throws IOException; //读取数据
- 从某处(某处取决于构造入参in)读取数据
- 每次读取一个字节
- 返回读取的数据
- 读到文件尾返回-1
- 读到文件尾后继续读依旧返回-1
BufferedInputStream使用read()读取数据示例
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class M10 {
public static void main(String[] args) {
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\gaogu\\Desktop\\iotest.txt"));
int b;
while ((b = bufferedInputStream.read()) != -1) {
System.out.println((char)b);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1.3.3. public int read(byte b[], int off, int len) throws IOException; //读取数据
- 每次读len个字节的数据放入b[off]到b[off+len]中,不足len个字节时剩多少个读多少个
- 返回本次读到了多少个字节
- 当本次读取到0个字节时返回-1
- public int read(byte b[]) == public int read(byte b[], 0, b.length)
BufferedInputStream使用read()读取数据示例
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class M11 {
public static void main(String[] args) {
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\gaogu\\Desktop\\iotest.txt"));
int b;
byte[] bytes = new byte[6]; //一般是1024的整数倍
while ((b = bufferedInputStream.read(bytes)) != -1) {
for (int i = 0; i < b; i++) {
System.out.println((char) bytes[i]);
}
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1.3.4. close(); //关闭流
1.4. BufferedOutputStream
- 带缓冲区的输出流
1.4.1. public BufferedOutputStream(OutputStream out, int size); //创建缓冲输出流
- 用来向某处(某处取决于入参out)写出数据
- 入参size是缓冲区大小(单位:字节)
- public BufferedOutputStream(OutputStream out) == public BufferedOutputStream(OutputStream out, 8192)
1.4.2. public synchronized void write(int b) throws IOException //写出数据
- 向某处(某处取决于构造入参out)写出数据
- 往文件尾部追加数据
- 每次写出一个字节到文件中
- 截取入参b低位一个字节写出到文件中
BufferedOutputStream使用write(int b)写出数据示例
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class M12 {
public static void main(String[] args) {
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\gaogu\\Desktop\\iotest.txt"));
bufferedOutputStream.write((int)'a');
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedOutputStream != null) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1.4.3. public void write(byte b[], int off, int len) throws IOException //写出数据
- 往文件尾部追加数据
- 每次将b[off]到b[off+len]的数据写出到文件
- public void write(byte b[]) == public void write(byte b[], 0, b.length)
BufferedOutputStream使用write(byte b[], int off, int len)写出数据到文件示例
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class M13 {
public static void main(String[] args) {
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\gaogu\\Desktop\\iotest.txt"));
byte[] bytes = new byte[] {(byte)'a',(byte)'b',(byte)'c',(byte)'d',(byte)'e',(byte)'f',(byte)'g'};
bufferedOutputStream.write(bytes);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedOutputStream != null) {
try {
bufferedOutputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
1.4.4. close() //关闭流
1.4.5. flush() //刷心缓冲区
- 往文件尾部追加数据
- 字符流
2.1. FileReader
- 自带8192byte的缓冲区,文件内容先会读取到缓冲区中再从缓冲区中读取数据
2.1.1publc FileReader(String fileName) throws FileNotFoundException;//创建文件输入流
- 用来读取本地文件中的数据(仅适用于纯文本文件)
- 入参fileName是文件地址
- 当文件路径不存在会报错java.io.FileNotFoundException
- 当文件不存在时会报错java.io.FileNotFoundException
2.1.2.public void close() throws IOException;//关闭流
2.1.3.public int read() throws IOException;//读取文件内容
- 每次读取一个字符
- 返回读取的数据
- 读到文件尾返回-1
- 读到文件尾后继续读依旧返回-1
FileReader使用read()读取文件示例
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class M05 {
public static void main(String[] args) {
FileReader fileReader = null;
try {
fileReader = new FileReader("C:\\Users\\gaogu\\Desktop\\iotest.txt");
int b;
while ((b = fileReader.read()) != -1) {
System.out.println((char)b);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2.1.4. public int read(char cbuf[], int offset, int length) throws IOException; //读取文件内容
- 每次读length个字符的数据放入cbuf[offset]到cbuf[offset+length]中,不足lenth个字符时剩多少个读多少个
- 返回本次读到了多少个字符
- 当本次读取到0个字符时返回-1
- public int read(char cbuf[]) == public int read(char cbuf[], 0, cbuf.length)
FileReader使用read(char cbuf[], int offset, int length)读取文件示例
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class M06 {
public static void main(String[] args) {
FileReader fileReader = null;
try {
fileReader = new FileReader("C:\\Users\\gaogu\\Desktop\\iotest.txt");
int b;
char[] chars = new char[6];
while ((b = fileReader.read(chars)) != -1) {
System.out.println(new String(chars,0,b));
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2.2. FileWriter
- 自带8192byte的缓冲区,文件内容先会写出到缓冲区中再从缓冲区写出到文件中
测试带缓冲区的流读取文件过程中文件内容被修改对读取的影响
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class HaveBufferTest {
public static void main(String[] args) throws IOException {
//先创建一个文件
FileWriter fileWriter0 = new FileWriter("C:\\Users\\gaogu\\Desktop\\iotest.txt");
for (int i = 0; i < 8192; i++) {
fileWriter0.write('a');
}
fileWriter0.write("bc");
fileWriter0.close();
//读取文件
FileReader fileReader = new FileReader("C:\\Users\\gaogu\\Desktop\\iotest.txt");
for (int i = 0; i < 8192; i++) {
char read = (char) fileReader.read();
System.out.println(read);
}
//读取文件过程中修改文件内容
FileWriter fileWriter = new FileWriter("C:\\Users\\gaogu\\Desktop\\iotest.txt");
for (int i = 0; i < 8192; i++) {
fileWriter.write('a');
}
fileWriter.write("defg");
fileWriter.flush();
//读取过程中修改文件对读取的影响
System.out.println((char) fileReader.read());
System.out.println((char) fileReader.read());
System.out.println((char) fileReader.read());
System.out.println((char) fileReader.read());
System.out.println(fileReader.read());
fileWriter.close();
fileReader.close();
}
}
2.2.1. public FileWriter(String fileName, boolean append) throws IOException; //创建输出流
- 用来写出数据到本地文件中(仅适用于纯文本文件)
- 入参fileName是文件地址
- 入参append表示是否开启续写,false表示创建文件输出流时会清空文件内容,true则不会清空文件内容
- 当文件路径不存在会报错java.io.FileNotFoundException
- 当文件路径存在但文件不存在时会创建文件
- public FileWriter(String fileName) == public FileWriter(String fileName, false)
2.2.2. public void close() throws IOException; //关闭流
2.2.3. public void write(int c) throws IOException; //写出数据到文件中
- 往文件尾部追加数据
- 每次写出一个字符到文件中
- 将字符在字符集中的编号通过编码集编码后写出到文件中
FileWriter使用write(int c)写出数据到文件示例
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class M07 {
public static void main(String[] args) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("C:\\Users\\gaogu\\Desktop\\iotest.txt");
fileWriter.write((int)'我'); //向文件中写出一个字符(将字符在字符集中的编号通过编码集编码后写入文件中)
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2.2.4. public void write(char cbuf[], int off, int len) throws IOException; //写出数据到文件中
- 往文件尾部追加数据
- 每次将cbuf[off]到cbuf[off+len]的字符写出到文件
- public void write(char cbuf[]) == public void write(char cbuf[], int 0, cbuf.len)
FileWriter使用write(char cbuf[], int off, int len)写出数据到文件示例
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class M08 {
public static void main(String[] args) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("C:\\Users\\gaogu\\Desktop\\iotest.txt");
char[] chars = new char[] {'1','2','3','a','b','c','中','国'};
fileWriter.write(chars);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2.2.5. public void write(String str, int off, int len) throws IOException; //写出数据到文件中
- 往文件尾部追加数据
- 每次将cbuf.charAt(off)到cbuf.charAt(off+len)的字符写出到文件
- public void write(String str, 0, str.length()) == public void write(String str, 0, str.length())
FileWriter使用write(char cbuf[], int off, int len)写出数据到文件示例
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class M09 {
public static void main(String[] args) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("C:\\Users\\gaogu\\Desktop\\iotest.txt");
String str = "123abc中国";
fileWriter.write(str);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2.2.6. public void flush() throws IOException; //缓冲区数据写出到文件中
2.3. BufferedReader
- 带有缓冲区的输入流
2.3.1. public BufferedReader(Reader in, int sz); //创建缓冲输入流
- 用来从某处(某处取决于入参in)读取数据(仅适用于纯文本文件)
- 入参sz是缓冲区大小(单位:char)
- 字符基本流一般都有缓冲区,此时当高级流的入参是字符基本流时就有了双缓冲区
- public BufferedReader(Reader in) == public BufferedReader(Reader in, 8192)
2.3.2. public void close() throws IOException; //关闭流
2.3.3. public String readLine() throws IOException; //读取一行数据
- 每次读取一行数据(读取数据读到回车符本次结束),读取到的内容不包含回车符
- 读到末尾返回null
- 读到末尾继续读依旧返回null
BufferedReader使用readLine()读取数据示例
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class M14 {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("C:\\Users\\gaogu\\Desktop\\iotest.txt"));
int b;
String str;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2.4. BufferedWriter
- 带有缓冲区的输出流
2.4.1. public BufferedWriter(Writer out, int sz); //创建缓冲输出流
- 用来向某处(某处取决于入参out)写出数据(仅适用于纯文本文件)
- 入参sz表示缓冲区大小(单位:char)
- 字符基本流一般都有缓冲区,此时当高级流的入参是字符基本流时高级流就有了双缓冲区
- public BufferedWriter(Writer out) == public BufferedWriter(Writer out, 8192)
2.4.2. public void newLine() throws IOException; //输出回车符
BufferedWriter使用newLine()写出回车符示例
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
public class M15 {
public static void main(String[] args) {
BufferedWriter bufferedReader = null;
try {
bufferedReader = new BufferedWriter(new FileWriter("C:\\Users\\gaogu\\Desktop\\iotest.txt"));
bufferedReader.write((int) 'a');
bufferedReader.newLine(); //输出回车符
bufferedReader.write("bc");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
2.5. InputStreamReader
- 带缓冲区的输入流
- 以指定的字符编码集读取数据
2.5.1. public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException; //创建转换输入流
- 用来从某处(某处取决于入参in)读取数据(仅适用于纯文本文件)
- 入参charsetName表示字符编码集
- 字符基本流一般都有缓冲区,此时当高级流的入参是字符基本流时高级流就有了双缓冲区
2.6. OutputStreamWriter
- 带缓冲区的输出流
- 以指定的字符编码集写出数据
2.6.1. public OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException; //创建转换输出流
- 用来向某处(某处取决于入参out)写出数据(仅适用于纯文本文件)
- 入参charsetName表示字符编码集
- 字符基本流一般都有缓冲区,此时当高级流的入参是字符基本流时高级流就有了双缓冲区

浙公网安备 33010602011771号