其实对于前面的字节输入输出流有一个缺陷(最后一个图片复制例子是规范的),就是写的不够规范,为什么呢就是我们需要关闭资源,不能一直让这个流占用资源,因此我们需要两种结构无论代码中间段是不是出错,都需要执行最后一个关闭资源的流程(下面介绍一下两种方法!)
try-catch-finally(专业方法,但是感觉有点臃肿):
package IoDemo;import java.io.*;public class Test01 {public static void main(String[] args) {//首先你要进行传输数据,你需要进行连接管道的创建InputStream fileInputStream = null;try {fileInputStream = new FileInputStream("src\\IoDemo\\node.txt");File file = new File("src\\IoDemo\\node.txt");long size = file.length();byte[] buffer = new byte[(int) size];//这里就给我了一些警告,要是真的太大了也没办法进行处理int len = fileInputStream.read(buffer);System.out.println(new String(buffer, 0, len));System.out.println(size);System.out.println(len);} catch (Exception e) {throw new RuntimeException(e);}finally {//为了避免空指针,就是你需要判断不为null才关闭if (fileInputStream != null){try {fileInputStream.close();//关闭资源} catch (IOException e) {throw new RuntimeException(e);}}}}
}
try-with-catch(根据简约):上述其实释放资源的时候会显得很麻烦,如果我们事先定义好需要释放的资源后,那就不需要对其finally释放资源了:try(里面写你需要进行释放的流对象,占用的资源)-catch
package IoDemo;import java.io.*;public class Test01 {public static void main(String[] args) {//首先你要进行传输数据,你需要进行连接管道的创建try (InputStream fileInputStream = new FileInputStream("src\\IoDemo\\node.txt");){File file = new File("src\\IoDemo\\node.txt");long size = file.length();byte[] buffer = new byte[(int) size];//这里就给我了一些警告,要是真的太大了也没办法进行处理int len = fileInputStream.read(buffer);System.out.println(new String(buffer, 0, len));System.out.println(size);System.out.println(len);} catch (Exception e) {throw new RuntimeException(e);}}
}
【那么就需要对其进行注意就是,你怎么判断这个是不是资源(查看他是不是继承AutoCloseable接口就行),是资源才会放进去try()里面】
字符流:
(读写文本内容)--前面我们知道字节流是比较适合复制文件的,但是对于读取文件内容是不好的!(对于英文来说一个字符占用一个字节,汉字来说就是一个字符占用三个字节)
字符输入流:(硬盘写入到内存中)
package IoCharDemo;import java.io.FileNotFoundException;
import java.io.FileReader;public class Test01 {public static void main(String[] args) {try (//首先我们还是创建输入管道FileReader fileReader = new FileReader("src\\IoCharDemo\\note01.txt")){System.out.println("-----------方案1------------");//一次只读一个字符这样循环的调用是很浪费资源的!
// int c;
// while ((c = fileReader.read()) != -1){
// System.out.print((char)c);
// }System.out.println("-----------方案2------------");//这次就创建一个字符数组进行作为桶char[] buffer = new char[3];int len;while ((len = fileReader.read(buffer)) != -1){System.out.print(new String(buffer,0,len));}} catch (Exception e) {throw new RuntimeException(e);}}
}
字符输出流:(内存中写到硬盘中):
package IoCharDemo;import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;public class Test02 {public static void main(String[] args) {try (Writer fileWriter = new FileWriter("src\\IoCharDemo\\note02.txt",true);//后面有true就是追加){//写一个字符fileWriter.write('a');fileWriter.write('6');fileWriter.write('好');fileWriter.write("\r\n");//字符串fileWriter.write("我爱你中国!");fileWriter.write("我爱你中国!",0,3);fileWriter.write("\r\n");//字符数组char[] buffer = {'我','d','2'};fileWriter.write(buffer);fileWriter.write(buffer,0,2);fileWriter.write("\r\n");} catch (IOException e) {throw new RuntimeException(e);}}
}
【这里需要注意就是你必须关闭流,才能对文件里面进行写入数据!,输入数据是不需要,因为内存对于硬盘来很快,所以从硬盘到内存的输入操作不要关闭资源,但是对于内存到硬盘来说,硬盘太慢了不能让内存等他吧!,所以就需要个缓存池进行,等到资源关闭后,一起写入文件里面】