文章目录
前言
今天看到了一些关于Java IO流的问题,在之前学校老师浅显的讲过(可能讲的深过,咳咳、、我觉得没认真听的应该不止我一个吧),今天一下我我查了好多大佬的解析,有感写篇文章记录一下知识,以便于下次复习!
一、IO流的关系图
先上图,来源于菜鸟编程(很多大佬都用这张图)
关系上猛一看,我的乖乖,这么多。但是,当我们仔细看一下,嗯··还是这么多,不挣扎了,干吧!
二、IO流分类
1.按照流向可分为输入流和输出流
1.输入流: 只能从中读取数据,而不能向其写入数据。
2.输出流:只能向其写入数据,而不能向其读取数据。
2 按照操作单元划分可以划分为字节流和字符流
字节流:每次读取(写出)一个字节,当传输的资源文件有中文时,就会出现乱码
字符流:每次读取(写出)两个字节,有中文时,使用该流就可以正确传输显示中文。
这两个用起来区别不是很大,主要区别在于字节流和字符流所操作的数据单元不同,字节流操作的单元是数据单元是8位的字节,字符流操作的是数据单元为16位的字符。,只要记得一般读取文本文件的时候,最好选择字符流就行了。
代码如下(示例):
三、IO 常用方法
在InputStream里面包含如下3个方法。
int read(); 从输入流中读取单个字节(相当于从图15.5所示的水管中取出一滴水),返回所读取的字节数据(字节数据可直接转换为int类型)。
int read(byte[] b)从输入流中最多读取b.length个字节的数据,并将其存储在字节数组b中,返回实际读取的字节数。
int read(byte[] b,int off,int len); 从输入流中最多读取len个字节的数据,并将其存储在数组b中,放入数组b中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字节数。
在Reader中包含如下3个方法。
int read(); 从输入流中读取单个字符(相当于从图15.5所示的水管中取出一滴水),返回所读取的字符数据(字节数据可直接转换为int类型)。
int read(char[] b)从输入流中最多读取b.length个字符的数据,并将其存储在字节数组b中,返回实际读取的字符数。
int read(char[] b,int off,int len); 从输入流中最多读取len个字符的数据,并将其存储在数组b中,放入数组b中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字符数。
utputStream和Writer:
OutputStream和Writer的用法也非常相似,它们采用如图15.6所示的模型来执行输入,两个流都提供了如下三个方法:
void write(int c); 将指定的字节/字符输出到输出流中,其中c即可以代表字节,也可以代表字符。
void write(byte[]/char[] buf); 将字节数组/字符数组中的数据输出到指定输出流中。
void write(byte[]/char[] buf, int off,int len ); 将字节数组/字符数组中从off位置开始,长度为len的字节/字符输出到输出流中。
因为字符流直接以字符作为操作单位,所以Writer可以用字符串来代替字符数组,即以String对象作为参数。Writer里面还包含如下两个方法。
void write(String str); 将str字符串里包含的字符输出到指定输出流中。
void write (String str, int off, int len); 将str字符串里面从off位置开始,长度为len的字符输出到指定输出流中。
四、代码实现
1.字节流的文件读取
下面展示一些代码
import java.io.FileInputStream;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
String filepath="D:\\javademo\\file\\test.txt";
FileInputStream fis=null;
try {
//创建字节输入流
fis=new FileInputStream(filepath);
//创建一个长度为1024
byte[] b=new byte[1024];
//用于保存的实际字节数
int hasRead=0;
//使用循环来重复获取的过程
while((hasRead=fis.read(b))>0){
System.out.print(new String(b,0,hasRead));
}
}catch (IOException e){
e.printStackTrace();
}finally {
fis.close();
}
}
}
2.字节流的文件写入
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
String filepath = "D:\\javademo\\file\\test.txt";
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//创建字节输入流
fis = new FileInputStream(filepath);
//创建字节输出流
fos = new FileOutputStream("D:\\javademo\\file\\newTest2.txt");
byte[] b = new byte[1024];
int hasRead = 0;
//循环从输入流中取出数据
while ((hasRead = fis.read(b)) > 0) {
//每读取一次,即写入文件输入流,读了多少,就写多少。
fos.write(b, 0, hasRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
fis.close();
fos.close();
}
}
}
3.字符流的文件读取
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class test {
public static void main(String[] args) throws IOException {
String filepath = "D:\\javademo\\file\\test.txt";
FileReader fis=null;
try {
//创建字节输入流
fis=new FileReader(filepath);
//创建一个长度为1024的竹筒
char[] b=new char[1024];
//用于保存的实际字节数
int hasRead=0;
//使用循环来重复取水的过程
while((hasRead=fis.read(b))>0){
//将字节数组转换成字符串进行输出
System.out.print(new String(b,0,hasRead));
}
}catch (IOException e){
e.printStackTrace();
}finally {
fis.close();
}
}
}
3.字符流的文件写入
import java.io.*;
public class test {
public static void main(String[] args) throws IOException {
String filepath = "D:\\javademo\\file\\test.txt";
FileReader fis = null;
FileWriter fos = null;
try {
//创建字节输入流
fis = new FileReader(filepath);
//创建字节输出流
fos = new FileWriter("D:\\javademo\\file\\newTest4.txt");
char[] b = new char[1024];
int hasRead = 0;
//循环从输入流中取出数据
while ((hasRead = fis.read(b)) > 0) {
//每读取一次,即写入文件输入流,读了多少,就写多少。
fos.write(b, 0, hasRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
fis.close();
fos.close();
}
}
}
5.使用FileReader类、BufferedReader类、FileInputStream类三种方式读取和写入
package wx.day15.denno5;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class test {
// * 按字节(byte)读写
public static void FileInputStream_demo(String src,String dest) throws IOException{
FileInputStream in=new FileInputStream(src);
File file=new File(dest);
if(!file.exists()) file.createNewFile();
FileOutputStream out=new FileOutputStream(file);
// read() 从此输入流中读取一个数据字节。
// read(byte[] b) 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
int c;
byte buffer[]=new byte[1024];
while((c=in.read(buffer))!=-1){
for(int i=0;i<c;i++) out.write(buffer[i]);
}
in.close();
out.close();
}
// * 按字符(char)读写
public static void FileReader_demo(String src,String dest) throws IOException{
FileReader in = new FileReader(src);
File file = new File(dest);
if (!file.exists()) file.createNewFile();
FileWriter out= new FileWriter(file);
char[] buffer = new char[3];
int c = 0;
while ((c = in.read(buffer)) != -1) {
out.write(buffer,0,c);
}
in.close();
out.close();
}
// * 按字符读写,或者字符串
public static void BufferedReader_demo(String src,String dest) throws IOException{
File file_in = new File(src);
BufferedReader in=new BufferedReader(new FileReader(file_in));
File file_out = new File(dest);
if (!file_out.exists()) file_out.createNewFile();
BufferedWriter out=new BufferedWriter(new FileWriter(file_out));
//read() 读取单个字符。
//readLine() 读取一个文本行。
char[] buffer = new char[3];
int c = 0;
while ((c = in.read(buffer)) != -1) {
out.write(buffer,0,c);
}
in.close();
out.close();
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String src="D:\\javademo\\file\\test.txt";
String out1="D:\\javademo\\file\\5_test1.txt";
String out2="D:\\javademo\\file\\5_test2.txt";
String out3="D:\\javademo\\file\\5_test3.txt";
FileInputStream_demo(src,out1);
FileReader_demo(src,out2);
BufferedReader_demo(src,out3);
}
}
总结
关于IO流的知识,鄙人只能浅显的总结到这里了,日后再有更深的理解会继续改进,完善。继续搬砖去了!