2021.10.26 晴
IO流:
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。
I : input 输入
O : output 输出
输入 : 就是把数据输入到内存中
输出 : 就是把内存中的数据写出到外面
按处理数据类型的不同,分为字节流和字符流
按数据流向的不同,分为输入流和输出流。(入和出是相对于内存来讲的)
按功能不同,分为节点流和处理流
- 节点流:直接操作数据源
- 处理流:对其他流进行处理
InputStream : 字节输入
OutputStream : 字节输出
Reader : 字符输入
Writer : 字符输出
InputStream
read : 读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾(读完了)返回-1
package study;
import java.io.FileInputStream;
/**
* FileInputStream 是字节输入流,就是读取文件中的数据
*
* 操作系统层面 一切皆文件
* @author 落华见樱
*
*/
public class Part01 {
public static void main(String[] args) throws Exception {
// 要读取文件,必须要找到它
// 如何找? 两种方式 : 绝对路径 和 相对路径
// 绝对路径 : 能够找到这个文件的全路径
// 相对路径 : 相对当前文件所在位置去找其他文件 , ./ 表示当前目录, ../ 表示上级目录, ../../ 表示上上级目录, ../../../...
// FileInputStream fis = new FileInputStream("D:/test/a.txt");
// eclipse中 ./ 找到的是当前项目
FileInputStream fis = new FileInputStream("./src/study/Part01.java");
// read : 读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾(读完了)返回-1
int i1 = fis.read();
System.out.println((char)i1);
}
}
package study;
import java.io.FileInputStream;
/**
* 循环读取
* @author 落华见樱
*
*/
public class Part02 {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("./src/study/Part01.java"); ){
int temp = 0;
while( (temp = fis.read()) != -1 ){
System.out.print((char)temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
read方法重载
package study;
import java.io.FileInputStream;
/**
* read方法的重载 , 可以传递一个字节数组,为了提高读取效率
*
* 默认一次读取一个字节,如果传递了字节数组,则把数组一次性读满,再回来,然后下次去再读满,一直到读完
*
* read(byte[]) : 一次读取一个数组,返回当前次读取到的个数,如果到达文件末尾,返回-1
*
* @author 落华见樱
*
*/
public class Part03 {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream(
"./src/study/Part01.java");) {
// available : 可以获取的字节数
System.out.println(fis.available());
// byte[] bytes = new byte[fis.available()];
byte[] bytes = new byte[1025];
int count = fis.read(bytes);
System.out.println(new String(bytes, 0, count));
System.out.println(count);
count = fis.read(bytes);
// new String(bytes) 把自己数组中所有的数据转换为字符串,但是可能有冗余数据
// 所以推荐使用 new String(bytes,0,count) 只把本次读取的数据转换为字符串
System.out.println(new String(bytes, 0, count));
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
}
}
}
package study;
import java.io.FileInputStream;
/**
* 循环读取
* @author 落华见樱
*
*/
public class Part04 {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream(
"./src/study/Part01.java");) {
byte[] bytes = new byte[1025];
int count = 0;
while ((count = fis.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, count));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
FileReader
package study;
import java.io.FileReader;
/**
* FileReader 字符输入流, 字节输入流是一次读取一个字节,而字符输入流一次读取一个字符
*
* 并且java中汉字采用的是unicode编码,而unicode编码占用16位,就是一个字符
*
* 所以使用字符输入流可以解决汉字乱码问题
*
* 字符流一般只用于纯文本文件,比如压缩包,图片,视频等,还是需要使用字节流处理
*
* read() : 读取一个字符,返回该字符的int值,到达文件末尾返回 -1
*
* read(char[]) : 读取一个char数组,返回当前次读取的个数,到达末尾返回-1
* @author 落华见樱
*
*/
public class Part05_FileReader {
public static void main(String[] args) {
try (FileReader fr = new FileReader(
"./src/study/Part01.java")) {
int temp = 0;
while ((temp = fr.read()) != -1) {
System.out.print((char) temp);
}
} catch (Exception e) {
e.printStackTrace();
}
// try (FileReader fr = new FileReader(
// "./src/study/Part01.java")) {
// char[] chars = new char[512];
// int temp = 0;
// while ((temp = fr.read(chars)) != -1) {
// System.out.println(new String(chars, 0, temp));
// }
//
// } catch (Exception e) {
// e.printStackTrace();
// }
}
}
FileOutputStream
package study;
import java.io.FileOutputStream;
/**
* 输出流 是把内存中的数据写出到硬盘中
*
* 如果该文件不存在,会自动创建,但是不会创建文件夹
*
* 如果对应的文件夹目录不存在,就报错
*
* 构造方法
*
* FileOutputStream(String path) : 向该文件中写出数据,并覆盖原有内容
*
* FileOutputStream(String path,boolean append) : 向该文件中写出数据,如果append为true就追加写出,如果为false就覆盖
*
* write(int) : 写出一个数据
* write(byte[] b) : 写出该数组中所有内容
* write(byte[] b,int off,int len) : 写出该数组中指定数据
*
* flush() : 刷缓存,不调用也可以,关闭流的时候会自动调用
*
* 辅助方法 : getBytes() : String中的方法,用于获取字符串对应的字节数组
*
* @author 落华见樱
*
*/
public class Part06_FileOutputStream {
public static void main(String[] args) {
try (
// FileOutputStream fos = new FileOutputStream("F:/a.txt");
FileOutputStream fos = new FileOutputStream("F:/a.txt",true);
){
fos.write(97);
fos.write(98);
fos.write(99);
fos.write(100);
fos.write('\n');
String str = "你好吗";
byte[] bytes = str.getBytes();
fos.write(bytes);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
FileWriter
package study;
import java.io.FileWriter;
/**
* 字符型输出流
* @author 落华见樱
*
*/
public class Part07_FileWriter {
public static void main(String[] args) {
try (
FileWriter fw = new FileWriter("F:/a.txt");
){
// 写出字符串
fw.write("你好吗?\n");
char[] chars = {'a','b','c','d'};
// 写出char数组
fw.write(chars);
fw.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedInputStream
package study;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
/**
* 字节输入缓冲流
* @author 落华见樱
*
*/
public class Part08_BufferedInputStream {
public static void main(String[] args) {
// long startTime = System.currentTimeMillis();
// try (
// FileInputStream fis = new FileInputStream("F:/a.txt");
// ){
// byte[] bytes = new byte[1024];
// int temp = 0;
// while((temp=fis.read(bytes)) != -1){
// // System.out.println(new String(bytes,0,temp));
// }
// long endTime = System.currentTimeMillis();
// System.out.println("读取完成,耗时 : "+(endTime-startTime));
// 字节流 : 460
long startTime = System.currentTimeMillis();
try (
FileInputStream fis = new FileInputStream("F:/a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
){
byte[] bytes = new byte[1024];
int temp = 0;
while((temp=bis.read(bytes)) != -1){
// System.out.println(new String(bytes,0,temp));
}
long endTime = System.currentTimeMillis();
System.out.println("读取完成,耗时 : "+(endTime-startTime));
// 字节流 : 120
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedOutputStream
package study;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
/**
* 字节输出缓冲流
* @author 落华见樱
*
*/
public class Part09_BufferedOutputStream {
public static void main(String[] args) {
try (FileOutputStream fos = new FileOutputStream("F:/a.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
) {
bos.write(97);
bos.write(98);
bos.write(99);
bos.write(100);
bos.write('\n');
String str = "你好吗";
byte[] bytes = str.getBytes();
bos.write(bytes);
bos.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedReader:
package study;
import java.io.BufferedReader;
import java.io.FileReader;
/**
* 字符输入缓冲流
*
* 新增方法 String readLine() : 读取一行数据,返回值就是读到的数据,到达文件末尾返回 null
* @author 落华见樱
*
*/
public class Part10_BufferedReader {
public static void main(String[] args) {
try (FileReader fr = new FileReader("F:/a.txt");
BufferedReader br = new BufferedReader(fr);) {
String temp = null;
// 读取一行
while ((temp = br.readLine()) != null) {
System.out.println(temp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
BufferedWriter
package study;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* 字符输出缓冲流
*
* bw.newLine(); 作用为 换行
* @author 落华见樱
*
*/
public class Part11_BufferedWriter {
public static void main(String[] args) {
BufferedWriter bw = null ;
try {
bw = new BufferedWriter(new FileWriter("F:/a.txt"));
bw.write("你好吗");
// 换行
bw.newLine();
bw.write("我很好");
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}