package top; 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.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; /** * description: * * @author: dawn.he QQ: 905845006 * @email: dawn.he@cloudwise.com * @email: 905845006@qq.com * @date: 2019/8/19 10:45 PM */ public class iodemo { public static void main(String[] args) throws IOException { iodemo io = new iodemo(); // io.InputStream(); io.BufferedInputStream(); // io.InputStreamReader(); // io.BufferedReader(); // io.Reader(); } //InputStream、OutputStream(字節流) public void InputStream() throws IOException { //讀取文件(字節流) InputStream in = new FileInputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/1.txt"); //寫入相應的文件 OutputStream out = new FileOutputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/2.txt"); //讀取數據 //一次性取多少字節 byte[] bytes = new byte[2048]; //接受讀取的內容(n就表明的相關數據,只不過是數字的形式) int n = -1; //循環取出數據 while ((n = in.read(bytes, 0, bytes.length)) != -1) { //轉換成字符串 String str = new String(bytes, 0, n, "UTF-8"); //這裏能夠實現字節到字符串的轉換,比較實用 System.out.println(str); //寫入相關文件 out.write(bytes, 0, n); } //關閉流 in.close(); out.close(); } public void BufferedInputStream() throws IOException { //讀取文件(緩存字節流) BufferedInputStream in = new BufferedInputStream(new FileInputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/1.txt")); //寫入相應的文件 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/2.txt")); //讀取數據 //一次性取多少字節 byte[] bytes = new byte[2048]; //接受讀取的內容(n就表明的相關數據,只不過是數字的形式) int n = -1; //循環取出數據 while ((n = in.read(bytes, 0, bytes.length)) != -1) { System.out.println(n); //轉換成字符串 String str = new String(bytes, 0, n, "UTF-8"); System.out.println(str); //寫入相關文件 out.write(bytes, 0, n); } //清楚緩存 out.flush(); //關閉流 in.close(); out.close(); } public void InputStreamReader() throws IOException { //讀取文件(字符流) InputStreamReader in = new InputStreamReader(new FileInputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/1.txt"), "UTF-8"); //寫入相應的文件 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/2.txt")); //讀取數據 //循環取出數據 int len = -1; while ((len = in.read()) != -1) { System.out.print((char)len+" "); System.out.println(len); //寫入相關文件 out.write(len); } //清楚緩存 out.flush(); //關閉流 in.close(); out.close(); } public void BufferedReader() throws IOException { //讀取文件(字符流) BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/1.txt"), "UTF-8"));//這裏主要是涉及中文 //BufferedReader in = new BufferedReader(new FileReader("d:\\1.txt"))); //寫入相應的文件 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/2.txt"), "UTF-8")); //BufferedWriter out = new BufferedWriter(new FileWriter("d:\\2.txt")); //讀取數據 //循環取出數據 String str = null; while ((str = in.readLine()) != null) { System.out.println(str); //寫入相關文件 out.write(str); out.newLine(); } //清楚緩存 out.flush(); //關閉流 in.close(); out.close(); } public void Reader() throws IOException { //讀取文件(字符流) Reader in = new InputStreamReader(new FileInputStream("/Users/heliming/IdeaProjects/democloud/kotlindemo/1.txt"), "UTF-8"); //寫入相應的文件 PrintWriter out = new PrintWriter(new FileWriter("/Users/heliming/IdeaProjects/democloud/kotlindemo/2.txt")); //讀取數據 //循環取出數據 byte[] bytes = new byte[1024]; int len = -1; while ((len = in.read()) != -1) { System.out.print((char)len+" "); System.out.println(len); //寫入相關文件 out.write(len); } //清楚緩存 out.flush(); //關閉流 in.close(); out.close(); } }
(一)「字節」的定義 字節(Byte)是一種計量單位,表示數據量多少,它是計算機信息技術用於計量存儲容量的一種計量單位。 (二)「字符」的定義 字符是指計算機中使用的文字和符號,好比一、二、三、A、B、C、~!·#¥%……—*()——+、等等。 (三)「字節」與「字符」 它們徹底不是一個位面的概念,因此二者之間沒有「區別」這個說法。不一樣編碼裏,字符和字節的對應關係不一樣: ①ASCII碼中,一個英文字母(不分大小寫)佔一個字節的空間,一箇中文漢字佔兩個字節的空間。一個二進制數字序列,在計算機中做爲一個數字單元,通常爲8位二進制數,換算爲十進制。最小值0,最大值255。 ②UTF-8編碼中,一個英文字符等於一個字節,一箇中文(含繁體)等於三個字節。 ③Unicode編碼中,一個英文等於兩個字節,一箇中文(含繁體)等於兩個字節。 符號:英文標點佔一個字節,中文標點佔兩個字節。舉例:英文句號「.」佔1個字節的大小,中文句號「。」佔2個字節的大小。 ④UTF-16編碼中,一個英文字母字符或一個漢字字符存儲都須要2個字節(Unicode擴展區的一些漢字存儲須要4個字節)。 ⑤UTF-32編碼中,世界上任何字符的存儲都須要4個字節。 字節 (byte):8個二進制位爲一個字節(B),最經常使用的單位。計算機存儲單位通常用B,KB,MB,GB,TB,PB,EB,ZB,YB,BB來表示,它們之間的關係是: 1B(Byte字節)=8bit 1KB (Kilobyte 千字節)=1024B, 1MB (Mega byte 兆字節 簡稱「兆」)=1024KB, 1GB (Giga byte 吉字節 又稱「千兆」)=1024MB, 1TB (Tera byte 萬億字節 太字節)=1024GB,其中1024=2^10 ( 2 的10次方) 用一個txt文檔作實驗 1000個漢字---utf-8編碼格式---佔用2.95k,接近3k。這是由於utf-8編碼格式下1000個字符佔3000字節,至關於3000B,接近3k。 ---asci編碼格式下,2k ---unicode編碼格式下,2k