JAVA之I/O流
阿新 • • 發佈:2020-09-20
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) publicstatic 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)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+"毫秒"); }}