一、介绍
I/O
是Input/Output
的缩写- 输入input:读取外部数据(磁盘、光盘等存储设备数据)到程序(内存)中
- 输出output:将程序(内存)数据输出到磁盘、光盘等存储设备中
二、分类
1、按数据流的方向
- 输入流
- 输出流
2、按处理数据单位
- 字节流
- 即一次只读取一个字节,可以用来处理一切文件,但是一般用来处理:二进制文件、图片、音频、视频
- 字符流
- 一次读取一个字符,一般用来处理:文本类型文件;不能处理图像视频等非文本文件
3、按功能
- 节点流
- 直接操作数据读写的流类,如:
FileInputStream
- 处理流
- 不直接连接到数据源,而是连接已存在的流,通过对数据的处理提供更强大的功能,如:
BufferedInputStream
三、基类
1、字节流
- InputStream
- 是所有字节输入流的父类,抽象类
- 实现类:
FileInputStream
、BufferedInputStream
、ObjectInputStream
- OutputStream
- 是所有字节输出流的父类,抽象类
- 实现类:
FileOutputStream
、BufferedOutputStream
、ObjectOutputStream
1、字符流
- Reader
- 是所有字符输入流的父类,抽象类
- 实现类:
InputStreamReader
、FileReader
、BufferedReader
- Writer
- 是所有字符输出流的父类,抽象类
- 实现类:
OutputStreamWriter
、FileWriter
、BufferedWriter
、PrintWriter
四、具体实现
1、FileInputStream
final String inputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/inputFile.txt";
FileInputStream fis1=null;
FileInputStream fis2=null;
try{
fis1=new FileInputStream(inputPathname);
fis2=new FileInputStream(new File(inputPathname));
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(fis1!=null){
fis1.close();
}
if(fis2!=null){
fis2.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
2、FileOutputStream
- 字节输出流
extends OutputStream
final String inputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/inputFile.txt";
final String outputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/outputFile.txt";
FileInputStream fis1=null;
FileOutputStream fos1=null;
FileOutputStream fos2=null;
FileOutputStream fos3=null;
FileOutputStream fos4=null;
try{
fos1=new FileOutputStream(outputPathname);
fos2=new FileOutputStream(outputPathname,true);
fos3=new FileOutputStream(new File(inputPathname));
fos4=new FileOutputStream(new File(inputPathname),true);
fis1=new FileInputStream(inputPathname);
byte[] buffer=new byte[1024];
int len;
while((len=fis1.read(buffer))!=-1){
fos2.write(buffer,0,len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(fis1!=null){
fis1.close();
}
if(fos1!=null){
fos1.close();
}
if(fos2!=null){
fos2.close();
}
if(fos3!=null){
fos3.close();
}
if(fos4!=null){
fos4.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
3、BufferedInputStream
- 缓冲字节输入流
extends FilterInputStream
- 为了提高读效率而设计
final String inputPathname = "file/inputFile.txt";
BufferedInputStream bis1=null;
BufferedInputStream bis2=null;
try{
InputStream ips = getClass().getClassLoader().getResourceAsStream(inputPathname);
bis1 = new BufferedInputStream(ips);
bis2 = new BufferedInputStream(ips, 1024);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bis1!=null){
bis1.close();
}
if(bis2!=null){
bis2.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
4、BufferedOutputStream
- 缓冲字节输出流
extends FilterOutputStream
- 为了提高写效率而设计
final String inputPathname = "file/inputFile.txt";
final String outputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/outputFile.txt";
BufferedInputStream bis1=null;
BufferedOutputStream bos1=null;
BufferedOutputStream bos2=null;
try{
InputStream ips = getClass().getClassLoader().getResourceAsStream(inputPathname);
bos1 = new BufferedOutputStream(ips);
bos2 = new BufferedOutputStream(ips, 1024);
bis1=new BufferedInputStream(ips);
byte[] buffer=new byte[1024];
int len;
while((len = bis1.read(buffer)) != -1){
bos1.write(buffer,0,len);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(bis1!=null){
bis1.close();
}
if(bos1!=null){
bos1.close();
}
if(bos2!=null){
bos2.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
5、ObjectInputStream
- 字节输入流(对象反序列化流)
extends InputStream
- 对象的反序列化:对象输入流在读取了一组字节后按照其表示的结构将其还原为对象的过程
final String objectFileName = "file/objectFile.txt";
ObjectInputStream ois=null;
try{
InputStream ips = getClass().getClassLoader().getResourceAsStream(objectFileName);
ois = new ObjectInputStream(new BufferedInputStream(ips));
User user = (User)ois.readObject();
System.err.println(user.toString());
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(ois!=null){
ois.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
6、ObjectOutputStream
- 字节输出流(对象序列化流)
extends OutputStream
- 对象序列化:对象流按照 对象 的结构将其转换为一组字节这个过程
- 数据持久化:然后这组字节再经过io写入文件中,将数据写入文件(硬盘上)的这个过程
- 序列化的对象的注意事项:
- 实现序列化接口:
implements Serializable
,否则会抛出异常NotSerializableException
- 添加序列化版本号属性:
private static final long serialVersionUID = 1L;
- 不必需set、get方法
- 当对象的属性被
transient
修饰后,再进行对象序列化时,该属性的值就会被忽略,这意味着反序列化时得不到该属性的值;忽略不必要的属性,可以做到对象序列化时的"瘦身"效果,减少资源开销
final String objectFileName = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/objectFile.txt";
ObjectOutputStream oos=null;
try{
oos=new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(objectFileName)));
User outUser = new User("kimi",18);
oos.writeObject(outUser);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(oos!=null){
oos.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
7、InputStreamReader
- 字符输入流
extends Reader
- 转换流,用于连接字节输入流,将字节输入流转换成字符输入流
final String inputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/inputFile.txt";
InputStreamReader isr1=new InputStreamReader(new FileInputStream(inputPathname));
InputStreamReader isr2=new InputStreamReader(new FileInputStream(inputPathname),StandardCharsets.UTF_8);
InputStreamReader isr3=new InputStreamReader(new FileInputStream(inputPathname),Charset.forName(StandardCharsets.UTF_8));
String charsetName=isr1.getEncoding();
8、OutputStreamWriter
- 字符输出流
extends Writer
- 转换流,用于连接字节输出流,将字节输出流转换成字符输出流
final String outputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/outputFile.txt";
OutputStreamWriter osw1=new OutputStreamWriter(new FileOutputStream(outputPathname));
OutputStreamWriter osw2=new OutputStreamWriter(new FileOutputStream(outputPathname),StandardCharsets.UTF_8);
OutputStreamWriter osw3=new OutputStreamWriter(new FileOutputStream(outputPathname),Charset.forName(StandardCharsets.UTF_8));
String charsetName=osw1.getEncoding();
9、FileReader
- 字符输入流
extends InputStreamReader
FileInputStream
+ InputStreamReader
- 无法设置编码格式
final String inputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/inputFile.txt";
FileReader fr1=null;
FileReader fr2=null;
try{
fr1=new FileReader(inputPathname);
fr2=new FileReader(new File(inputPathname));
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(fr1!=null){
fr1.close();
}
if(fr2!=null){
fr2.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
10、FileWriter
- 字符输出流
extends OutputStreamWriter
FileOutputStream
+ OutputStreamWriter
- 无法设置编码格式
final String outputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/outputFile.txt";
FileWriter fw1=null;
FileWriter fw2=null;
FileWriter fw3=null;
FileWriter fw4=null;
try{
fw1=new FileWriter(outputPathname);
fw2=new FileWriter(outputPathname,true);
fw3=new FileWriter(new File(outputPathname));
fw4=new FileWriter(new File(outputPathname),true);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(fw1!=null){
fw1.close();
}
if(fw2!=null){
fw2.close();
}
if(fw3!=null){
fw3.close();
}
if(fw4!=null){
fw4.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
11、BufferedReader
final String inputPathname = "file/inputFile.txt";
BufferedReader br1=null;
BufferedReader br2=null;
try{
InputStream ips = getClass().getClassLoader().getResourceAsStream(inputPathname);
br1 = new BufferedReader(new InputStreamReader(ips, StandardCharsets.UTF_8));
br2 = new BufferedReader(new InputStreamReader(ips, StandardCharsets.UTF_8), 1024);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(br1!=null){
br1.close();
}
if(br2!=null){
br2.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
12、BufferedWriter
final String inputPathname = "file/inputFile.txt";
final String outputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/outputFile.txt";
BufferedReader br1=null;
BufferedWriter bw1=null;
BufferedWriter bw2=null;
try{
InputStream ips = getClass().getClassLoader().getResourceAsStream(inputPathname);
bw1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPathname), StandardCharsets.UTF_8));
bw2 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputPathname), StandardCharsets.UTF_8), 1024);
br1 = new BufferedReader(new InputStreamReader(ips, StandardCharsets.UTF_8));
String lineStr;
while((lineStr=br1.readLine())!=null){
bw1.write(lineStr);
bw1.newLine();
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(br1!=null){
br1.close();
}
if(bw1!=null){
bw1.close();
}
if(bw2!=null){
bw2.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
13、PrintWriter
- 字符输出流
extends Writer
- 具有自动行刷新功能
- 内部是
BufferedWriter
构建的还会默认具有缓冲功能
final String inputPathname = "file/inputFile.txt";
final String outputPathname = "D:/IDEA/Projects/myproject/io-demo/src/main/resources/file/outputFile.txt";
BufferedReader br=null;
PrintWriter pw1=null;
PrintWriter pw2=null;
PrintWriter pw3=null;
PrintWriter pw4=null;
PrintWriter pw5=null;
PrintWriter pw6=null;
PrintWriter pw7=null;
PrintWriter pw8=null;
try{
InputStream ips = getClass().getClassLoader().getResourceAsStream(inputPathname);
pw1 = new PrintWriter(outputPathname);
pw2 = new PrintWriter(outputPathname, StandardCharsets.UTF_8.name());
pw3 = new PrintWriter(new File(outputPathname));
pw4 = new PrintWriter(new File(outputPathname), StandardCharsets.UTF_8.name());
pw5 = new PrintWriter(new FileOutputStream(outputPathname));
pw6 = new PrintWriter(new FileOutputStream(outputPathname),true);
pw7 = new PrintWriter(new BufferedWriter(new FileWriter(outputPathname)));
pw8 = new PrintWriter(new BufferedWriter(new FileWriter(outputPathname)),true);
br = new BufferedReader(new InputStreamReader(ips, StandardCharsets.UTF_8));
File outputFile = new File(outputPathname);
if(!outputFile.exists()){
outputFile.createNewFile();
}
pw4 = new PrintWriter(outputFile, StandardCharsets.UTF_8.name());
String lineStr;
while((lineStr = br.readLine()) != null){
pw4.println(lineStr);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(br!=null){
br.close();
}
if(pw1!=null){
pw1.close();
}
if(pw2!=null){
pw2.close();
}
if(pw3!=null){
pw3.close();
}
if(pw4!=null){
pw4.close();
}
if(pw5!=null){
pw5.close();
}
if(pw6!=null){
pw6.close();
}
if(pw7!=null){
pw7.close();
}
if(pw8!=null){
pw8.close();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
write()
和print()
的区别?
write()
和print()
效果是一样的,底层都是调用了write()
,只是入参数据类型不一样;print()
提供了更丰富的入参类型就是帮我们转换了数据类型再调用write()
print()
和println()
的区别?
println()
就是在print()
的基础上会 多写出一个换行符、多调用一次flush()
,即println()
=自动换行 + 自动行刷新
五、注意事项