标签:一起 new net 没事 文件操作 try 定义 exce nts
目录
在曾今的面试中被问到了io流,nio,还有如今经常听到的netty,io流还能答上一点,其他的一概不知怎么回答;
最近在公司也没事干,正好学习到了io流,就来整理一下吧;
这是Java io流的类图,网上很多,我也是截图的网上的
为了直接练习,文中所有涉及到的文件都是先创建好的;
带有Reader,Writer的都属于字符流
char
数组根据码表映射字符,一次可能读多个字节。
直接对文件操作
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
可以对文本一行一行的读取,但是输出的时候需要自己进行隔行,不然数据都会紧紧的挨在一起;
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
方法,因为改代码段使用了缓冲,缓冲区的数据只有存满了才会自动发送,这里尾存满需,要调用该方法将缓冲区的数据发送出去,不然文本中会没有数据;
将字节流转换为字符流,在进行处理;
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();
}
直接读文件进行操作
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();
}
将文件操作流放入缓冲区再进行操作
因为是对文件直接操作,所以无法一行一行的读取;也没有那个方法;
/* //图片
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
方法;
将对象持久化到本地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流
输入流只能进行读操作,输出流只能进行写操作
char
数组,而字节流中全是用的byte
数组这里面的内容非常详细;
标签:一起 new net 没事 文件操作 try 定义 exce nts
原文地址:https://www.cnblogs.com/guoyuchuan/p/9515788.html