Java io 入门
目录
前言
在曾今的面试中被问到了io流,nio,还有如今经常听到的netty,io流还能答上一点,其他的一概不知怎么回答;
最近在公司也没事干,正好学习到了io流,就来整理一下吧;
这是Java io流的类图,网上很多,我也是截图的网上的
代码演练
为了直接练习,文中所有涉及到的文件都是先创建好的;
字符流
-
带有Reader,Writer的都属于字符流
-
一般是对纯文本进行操作,是以字符为单位读取文本文件,所以以下的实例都是定义的
char
数组 -
根据码表映射字符,一次可能读多个字节。
FileReader,FileWriter:
直接对文件操作
FileReader fileReader=null;
FileWriter fileWriter=null;
File readfile=new File("E:/Javaio"+File.separator+"Javastudy.txt");
File writefile=new File("E:/Javaio/a"+File.separator+"test.txt");
try {
fileReader= new FileReader(readfile);
fileWriter=new FileWriter(writefile);
char[] read=new char[1024];
int len=0;
while ((len=fileReader.read(read))!=-1){
fileWriter.write(read,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fileReader!=null){
fileReader.close();
}
if(fileWriter!=null){
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedReader,BufferedWriter:
将文件操作的流放入缓冲区,在进行处理
BufferedReader
可以对文本一行一行的读取,但是输出的时候需要自己进行隔行,不然数据都会紧紧的挨在一起;
try {
File reader = new File("E:/Javaio" + File.separator + "Javastudy.txt");
File writer = new File("E:/Javaio/a" + File.separator + "test.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(reader));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(writer));
char[] read=new char[1024];
int len;
while ((len=bufferedReader.read(read))!=-1){
bufferedWriter.write(read,0,len);
}
/* String line;
while ((line=bufferedReader.readLine())!=null){
bufferedWriter.write(line);
bufferedWriter.newLine();
}*/
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
注意:这里调用了flush
方法,因为改代码段使用了缓冲,缓冲区的数据只有存满了才会自动发送,这里尾存满需,要调用该方法将缓冲区的数据发送出去,不然文本中会没有数据;
InputStreamReader,OutputStreamWriter:
将字节流转换为字符流,在进行处理;
File reader = new File("E:/Javaio" + File.separator + "Javastudy.txt");
File writer = new File("E:/Javaio/a" + File.separator + "test.txt");
try {
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(reader));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(writer));
char[] read=new char[1024];
int len;
while ((len=inputStreamReader.read(read))!=-1){
outputStreamWriter.write(read,0,len );
}
inputStreamReader.close();
outputStreamWriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
字节流
FileInputStream,FileOutputStream
直接读文件进行操作
try {
/* //图片
File inputstream = new File("E:/Javaio" + File.separator + "1.png");
File outputstream = new File("E:/Javaio/a" + File.separator + "1.png");*/
//音乐
File inputstream = new File("E:/Javaio" + File.separator + "test.mp3");
File outputstream = new File("E:/Javaio/a" + File.separator + "test.mp3");
if(outputstream.exists()){
outputstream.createNewFile();
}
FileInputStream fileInputStream = new FileInputStream(inputstream);
FileOutputStream fileOutputStream = new FileOutputStream(outputstream);
byte[] read=new byte[1024];
int len;
while ((len=fileInputStream.read(read))!=-1){
fileOutputStream.write(read,0,len);
}
fileInputStream.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
BufferedInputStream,BufferedOutputStream
将文件操作流放入缓冲区再进行操作
因为是对文件直接操作,所以无法一行一行的读取;也没有那个方法;
/* //图片
File inputstream = new File("E:/Javaio" + File.separator + "1.png");
File outputstream = new File("E:/Javaio/a" + File.separator + "1.png");*/
//音乐
File inputstream = new File("E:/Javaio" + File.separator + "test.mp3");
File outputstream = new File("E:/Javaio/a" + File.separator + "test.mp3");
if (outputstream.exists()) {
outputstream.createNewFile();
}
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(inputstream));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputstream));
byte[] read=new byte[1024];
int len;
while ((len=bufferedInputStream.read(read))!=-1){
bufferedOutputStream.write(read,0,len);
}
bufferedOutputStream.flush();
bufferedInputStream.close();
bufferedOutputStream.close();
注意:这里使用了缓冲,需要调用flush
方法;
ObjectOutputStream,ObjectInputStream
将对象持久化到本地TXT文件,注意,在TXT文件中的是二进制代码,并不是乱码;所以不要认为是乱码;
User user=new User();
user.setUsername("username");
user.setPassword("password");
File out=new File("E:/Javaio/a"+File.separator+"object.txt");
if(out.exists()){
out.createNewFile();
}
ObjectOutputStream outs=new ObjectOutputStream(new FileOutputStream(out));
outs.writeObject(user);
outs.close();
ObjectInputStream inputStream=new ObjectInputStream(new FileInputStream(out));
System.out.println(inputStream.readObject());
inputStream.close();
其他还有很多类和方法没有写,这里只进行入门的操作;代码部分就到这里了;
总结
io流
io流分类
- 根据流向来分:输入流和输出流
- 根据处理类型来分:字节流和字符流
输入输出流
输入流只能进行读操作,输出流只能进行写操作
字节流和字符流
- 字节流可以对所有文件进行操作,例如:图片,音乐文件,电影等;字符流只处理文本文档,例如TXT文本,xml,yml等
- 字节流是以字节为单位,字符流是以字符为单位,可以看到上边的字符流的代码中全是用的
char
数组,而字节流中全是用的byte
数组
这里面的内容非常详细;