java文件流、缓冲流、转换流、打印流
- 流
- 文件流
- FileInputStream
- FileOutputStream
- FileReader
- FileWriter
- 缓冲流
- BufferedInputStream
- BufferedOutputStream
- BufferedReader
- BufferedWriter
- 转换流
- 打印流
流
流是一组有顺序的,有七点有重点的字节集合,是对数据传输的总称或抽象。简单来说,就是设备之间的数据传递成为流。
流的本质:流的本质就是数据的传输,根据不同的数据传输特性,抽象出各种类,方便我们直接操作数据。
I:input输入
O:output输出
文件流
文件流主要包括:
- FileInputStream 字节输入
- FileOutputStream 字节输出
- FileReader 字符输入
- FileWriter 字符输出
字符流一般用于读取纯文本文件
字节流一般用于压缩包,图片等
方法都大同小异
FileInputStream
FileInputStream继承自InputStream,按照自己的方式在原始文件中读取数据。
相对路径:相对当前文件,如何找到
- ./ 当前目录
- …/ 上级目录
- / 下级目录
绝对路径:以系统跟目录为准,如何找到这个文件。
常用方法:
read():从输入流中读取下一个数据字节
read(byte[] b): 从输入流中读取一定量的字节并将其保存到数组b中,返回读取的长度,数据类型为long类型。
close():关闭流
available():可获得的字节数
skip(int n):跳过不读,返回long类型
如:读取一个当前文件
public class IO_06_FileInputStream {
public static void main(String[] args) throws IOException {
//创建流对象
FileInputStream fileInputStream = new FileInputStream("./day20_IO/src/practice/io/IO_06_FileInputStream.java");
//可获得的字节数
System.out.println(fileInputStream.available());
//读取字节数组
byte[] bytes = new byte[5];
fileInputStream.read(bytes);
System.out.println(new String(bytes));
System.out.println(fileInputStream.available());
//跳过不读
long skip = fileInputStream.skip(20);
System.out.println(skip);
System.out.println(fileInputStream.available());
//读取一个
System.out.println((char) fileInputStream.read());
//关闭资源
fileInputStream.close();
}
}
FileOutputStream
FileOutputStream继承自OutputStream,将程序中的内容写到硬盘中
输入流找不到指定文件会报错,但是输出流不会报错,会自动创建改文件,但是不会创建文件夹
构造方法:
- FileOutputStream(String):把内容输出到指定文件中,并会覆盖原文件中内容
- FileOutputStream(String,boolean):如果第二个参数是true,把内容输出到指定文件中,并在原文件中追加数据
常用方法:
- write(int i):写出整型
- write(byte[] b):写出字节数组,想要输出字符串可以利用字符串中的getBytes()方法,把字符串转为字节数组
- flush():刷缓存,强制把缓冲区写出,避免造成数据遗漏。
如:
public class IO_08_FileOutputStream {
public static void main(String[] args) {
//创建输出流对象,如果没有传递boolean值,默认是false,会替换源文件内容
//如果传入true,将会在文件末尾加入写出的数据
try {
FileOutputStream fileOutputStream = new FileOutputStream("E:/123.txt", true);
for (int i = 0; i < 26; i++) {
fileOutputStream.write(i + 97);
}
//写出字符串
String str = "Hello World哈哈哈哈";
byte[] b = str.getBytes(StandardCharsets.UTF_8);
fileOutputStream.write(b);
//刷缓存
fileOutputStream.flush();
//关闭连接
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileReader
FileReader:一次读一个字符,也就是2字节,Unicode编码也是2字节。
常用方法:
- read():读取一个字符,返回下一个字符数据,到达文件末尾返回-1
- read(byte[] b):读取一个字符数组,返回读取到的字符数,到达文件末尾返回-1
如:
public class IO_07_FileReader {
public static void main(String[] args) {
try {
FileReader fileReader = new FileReader("E:\\CodeLast\\workspaceForIDEA\\TL\\day20_IO\\src\\practice\\io\\IO_02_FileInputStream.java");
char[] c = new char[512];
int tmp = 0;
while ((tmp = fileReader.read(c)) != -1){
System.out.println(new String(c,0,tmp));
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileWriter
字符输出流
和字节输出流基本一致,新增字符串写出
如:
public class IO_09_FileWrite {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("E:/123.txt")) {
// \n是换行
fileWriter.write("哈哈哈哈哈\n");
char[] chars = {'a','b','c','d'};
fileWriter.write(chars);
fileWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲流
缓冲流就是包装流,创建对象的时候,传入的参数不是文件地址,而是文件流对象
作用:把每次读入的数据存入到一个缓冲区,然后一次写入;把输出的数据存入一个缓冲区,然后一次写出。
好处:提高效率。比如搬砖,没有缓冲流就是一块一块搬砖,加上缓冲流就是先装好一车,再运砖。
常用方法和文件流相同
BufferedInputStream
如:
public class IO_10_BufferedInputStream {
public static void main(String[] args) {
try {
//创建字节输入流
FileInputStream fileInputStream = new FileInputStream("./day20_IO/src/practice/io/IO_02_FileInputStream.java");
//创建字节输入缓冲流
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
//读取下一个字节,到达文件末尾返回-1
System.out.println((char) bufferedInputStream.read());
//读取一个数组,返回读到的长度,到达末尾返回-1
byte[] bytes = new byte[10];
int temp = bufferedInputStream.read(bytes);
System.out.println(new String(bytes,0,temp));
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedOutputStream
如:
public class IO_11_BufferedOutputStream {
public static void main(String[] args) {
try {
//创建字节输出流对象
FileOutputStream fileOutputStream = new FileOutputStream("E:/123.txt");
//创建字节输出缓冲流对象,并把字节输出流传入
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//写出单个
bufferedOutputStream.write(97);
//写出字符串
bufferedOutputStream.write("Hello World".getBytes(StandardCharsets.UTF_8));
//刷缓存
bufferedOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader
新增加了readLine(),作用是读取一行数据,返回值就是读到的这一行的数据,到达文件末尾,返回null。
如:
public class IO_12_BufferedReader {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("./day20_IO/src/practice/io/IO_02_FileInputStream.java");
BufferedReader bufferedReader = new BufferedReader(fileReader);
//读取一行,返回读到的内容,到达文件末尾返回null
String temp = bufferedReader.readLine();
System.out.println(temp);
//循环读取
String str = null;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
}
}
BufferedWriter
新增newLine(),作用是换行,就等于\n
如:
public class IO_13_BufferedWrite {
public static void main(String[] args) throws IOException {
//创建字符输出流,默认覆盖写出
FileWriter fileWriter = new FileWriter("E:/123.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("哈哈哈哈哈哈哈");
//换行
bufferedWriter.newLine();
bufferedWriter.write("哈哈哈哈哈哈哈哈哈哈");
//刷缓存
bufferedWriter.flush();
fileWriter.close();
}
}
转换流
转换流:字节流和字符流之间转换的桥梁,可以通过转换把字节流转换为字符流,并且还可以在转换的过程中,指定字符编码。
- InputStreamReader:输入流转换
- OutputStreamWriter:输出流转换
应用场景:接受别的地方的数据,比如接收到的是字节数据,自己需要使用字符数据
如:
public class IO_14_InputStreamReader {
public static void main(String[] args) throws IOException {
//创建一个字节输入流对象
FileInputStream fileInputStream = new FileInputStream("./day20_IO/src/practice/io/IO_02_FileInputStream.java");
//创建转换流对象
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//创建字符输入缓冲流,把转换流传入
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String temp = bufferedReader.readLine();
System.out.println(temp);
}
}
打印流
打印流是输出最方便的类
- 字节打印流PrintStream
- 字符打印流PrintWriter
使用方式:
public class IO_15_PrintStream {
public static void main(String[] args) throws FileNotFoundException {
//out是标准的打印流,打印到控制台
System.out.println("哈哈哈哈哈哈哈哈哈哈");
//创建输出流
FileOutputStream fileOutputStream = new FileOutputStream("E:/log.txt");
//创建打印流,并传入输出流
PrintStream printStream = new PrintStream(fileOutputStream);
//调用打印方法,打印到指定文件
printStream.println("日志系统");
//可以设置System中的out
System.setOut(printStream);
//设置到System中后,在使用打印语句,会打印到指定地方
System.out.println("=============================");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSSS");
System.out.println("m1开始执行:"+simpleDateFormat.format(new Date()));
m1();
System.out.println("m1执行结束:"+simpleDateFormat.format(new Date()));
}
public static void m1(){
System.out.println("m1 method execute!!");
}
}
执行完setOut(PrintStream out)方法后,所以控制台的输出都会输出到指定文件当中去。