1、File类
-
参考API文档
2、四个基本流
-
基于字节的输入流:InputStream-->FileInputStream
-
基于字节的输出流:OutputStream-->FileOutputStream
-
基于字符的输入流:Reader-->InputStreamReader
-
FileReader 继承于InputStreamReader
-
-
基于字符的输出流:Writer-->OutPutStreamWriter
-
FileWriter继承于OutPutStreamWriter
-
注意:
/* 输入流和输出流是站在内存的角度考虑数据的
-
-
输出流:数据从内存中输出。
-
缓存:BufferedReader 和BufferedWriter 这个是带有缓存的封装
3、两个缓冲流
BufferedReader
BufferedWriter
4、基于字节读取文件
import java.io.FileInputStream; //文件流(基于字节) public class Test3 { //以字节为单位读取文件“D:18级大数据方向Test.java”的内容,并输出到控制台 //输入流、基于字节(InputStream) public static void main(String[] args)throws Exception { FileInputStream fis = new FileInputStream("D:\18级大数据方向\Test.java"); //以字节为单位,进行读取 //单个字节读取 /* int byt; while((byt=fis.read()) != -1) { System.out.print((char)byt); } fis.close();//关闭流 */ //批量字节读取 byte[] bytes = new byte[1024]; int nums;//实际读到的字节个数 while((nums=fis.read(bytes)) != -1) { //本次读取到nums个字节,放在bytes数组里 //bytes[0]--bytes[nums-1] String s = new String(bytes,0,nums); System.out.print(s); } } }
5、基于字符读取文件
import java.io.FileInputStream; import java.io.FileReader; //文件流(基于字符) public class Test4 { public static void main(String[] args)throws Exception { //构建基于字符的输入流 FileReader fr = new FileReader("D:\18级大数据方向\Test.java"); //单字符读取 /* int n; while((n=fr.read()) != -1) { System.out.print((char)n); } fr.close(); */ //批量字符读取 char[] chars = new char[100]; int nums; while((nums=fr.read(chars)) != -1) { //读取到了nums个字符,放在chars数组里 String s = new String(chars,0,nums); System.out.print(s); } fr.close(); } }
6、基于字节写入文件
import java.io.FileOutputStream; public class Test5 { public static void main(String[] args)throws Exception { FileOutputStream fos = new FileOutputStream("D:\18级大数据方向\abc.txt"); String msg = "hello 中国!"; byte[] bytes = msg.getBytes(); //System.out.println(bytes.length); //单个字节写出 /* for(byte b:bytes) { fos.write(b); } */ //批量字节写出 fos.write(bytes); //fos.write(bytes, 0, bytes.length); fos.close(); } }
7、基于字符写入文件
import java.io.FileWriter; public class Test6 { public static void main(String[] args)throws Exception { FileWriter fw = new FileWriter("D:\18级大数据方向\abc.txt",true); String msg = "hello 中国!"; char[] chars = msg.toCharArray(); for(char c:chars) { fw.write(c); } fw.close(); } }
8、利用I/O流进行文本复制
P.S.参考了 徐同学coding 的博客(https://blog.csdn.net/weixin_36586120/article/details/80486112)
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; //基于单个字节的文本复制(未使用缓冲),fun1耗时为:1281毫秒 public class TestIOtime { public void fun1() throws IOException { FileInputStream fis = new FileInputStream("D:\18级大数据方向\Test.java");//185K FileOutputStream fos = new FileOutputStream("D:\桌面\Test.java"); int by = 0 ; while ((by = fis.read())!=-1) { fos.write(by); } fis.close(); fos.close(); } //基于字节数组的文本复制(未使用缓冲),fun1耗时为:2毫秒 public void fun2() throws IOException { FileInputStream fis = new FileInputStream("D:\18级大数据方向\Test.java"); FileOutputStream fos = new FileOutputStream("D:\桌面\Test2.java"); int length = 0; byte [] by =new byte[1024]; while ((length = fis.read(by))!=-1) { fos.write(by); } fis.close(); fos.close(); } //基于单个字节的文本复制(使用缓冲), fun3耗时为:9毫秒 public void fun3() throws IOException { FileInputStream fis = new FileInputStream("D:\18级大数据方向\Test.java"); BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos =new FileOutputStream("D:\桌面\Test3.java"); BufferedOutputStream bos =new BufferedOutputStream(fos); int by = 0; while ((by = bis.read())!=-1) { bos.write(by); } bis.close(); fis.close(); bos.close(); fos.close(); } //基于字节数组的复制(使用缓冲), fun4耗时为:1毫秒 public void fun4() throws IOException { FileInputStream fis = new FileInputStream("D:\18级大数据方向\Test.java"); BufferedInputStream bis = new BufferedInputStream(fis); FileOutputStream fos = new FileOutputStream("D:\桌面\Test4.java"); BufferedOutputStream bos = new BufferedOutputStream(fos); byte [] by = new byte[1024]; int length = 0; while ((length = bis.read(by))!=-1) { bos.write(by); } bis.close(); fis.close(); bos.close(); fos.close(); } //基于单个字符复制文本(未使用缓冲),fun5耗时为:49毫秒 public void fun5() throws IOException { FileReader fr = new FileReader("D:\18级大数据方向\Test.java"); FileWriter fw =new FileWriter("D:\桌面\Test5.java"); int n; while ((n = fr.read())!=-1) { fw.write(n); } fr.close(); fw.close(); } //基于单个字符复制文本(使用缓冲),fun6耗时为:17毫秒 public void fun6() throws IOException { FileReader fr = new FileReader("D:\18级大数据方向\Test.java"); BufferedReader br = new BufferedReader(fr); FileWriter fw =new FileWriter("D:\桌面\Test6.java"); BufferedWriter bw = new BufferedWriter(fw); int n; while ((n = br.read())!=-1) { bw.write(n); } br.close(); fr.close(); bw.close(); fw.close(); } //基于批量字符复制文本(未使用缓冲),fun7耗时为:10毫秒<char数组容量在100一下耗时会增高,100以上耗时波动不大> public void fun7() throws IOException { FileReader fr = new FileReader("D:\18级大数据方向\Test.java"); FileWriter fw =new FileWriter("D:\桌面\Test7.java"); char [] c = new char[100]; int n; while (( n = fr.read(c))!=-1) { fw.write(c); } fr.close(); fw.close(); } //基于批量字符复制文本(使用缓冲),fun8耗时为:4毫秒,同fun7,为啥这里感觉缓冲意义不大?原因因该出在字符数组上,总感觉哪里不对 public void fun8() throws IOException { FileReader fr = new FileReader("D:\18级大数据方向\Test.java"); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter("D:\桌面\Test8.java"); BufferedWriter bw = new BufferedWriter(fw); char []c = new char[100]; int length = 0; while ((length = br.read(c))!=-1) { bw.write(c); } br.close(); fr.close(); bw.close(); fw.close(); } public static void main(String[] args) throws IOException { long t1 = 0; long t2 = 0; t1 =System.currentTimeMillis(); new TestIOtime().fun1(); t2 = System.currentTimeMillis(); System.out.print("fun1耗时为:"); System.out.println(t2-t1+"毫秒"); t1 =System.currentTimeMillis(); new TestIOtime().fun2(); t2 = System.currentTimeMillis(); System.out.print("fun2耗时为:"); System.out.println(t2-t1+"毫秒"); t1 =System.currentTimeMillis(); new TestIOtime().fun3(); t2 = System.currentTimeMillis(); System.out.print("fun3耗时为:"); System.out.println(t2-t1+"毫秒"); t1 =System.currentTimeMillis(); new TestIOtime().fun4(); t2 = System.currentTimeMillis(); System.out.print("fun4耗时为:"); System.out.println(t2-t1+"毫秒"); t1 =System.currentTimeMillis(); new TestIOtime().fun5(); t2 = System.currentTimeMillis(); System.out.print("fun5耗时为:"); System.out.println(t2-t1+"毫秒"); t1 =System.currentTimeMillis(); new TestIOtime().fun6(); t2 = System.currentTimeMillis(); System.out.print("fun6耗时为:"); System.out.println(t2-t1+"毫秒"); t1 =System.currentTimeMillis(); new TestIOtime().fun7(); t2 = System.currentTimeMillis(); System.out.print("fun7耗时为:"); System.out.println(t2-t1+"毫秒"); t1 =System.currentTimeMillis(); new TestIOtime().fun8(); t2 = System.currentTimeMillis(); System.out.print("fun8耗时为:"); System.out.println(t2-t1+"毫秒"); }}