1.字元流 1.1 所有文件的存儲都是位元組的存儲,我們日常在磁碟上保存的其實並不是文件的字元,而是先把字元轉換成位元組,再把這些位元組儲存到磁碟。在讀取文件時,也是一個位元組一個位元組的讀取,然後生成位元組的序列。 1.2 位元組流可以處理任何對象,但是字元流呢,、只用來處理字元或者字元串。可以這樣說,位元組流呢 ...
1.字元流
1.1 所有文件的存儲都是位元組的存儲,我們日常在磁碟上保存的其實並不是文件的字元,而是先把字元轉換成位元組,再把這些位元組儲存到磁碟。在讀取文件時,也是一個位元組一個位元組的讀取,然後生成位元組的序列。
1.2 位元組流可以處理任何對象,但是字元流呢,、只用來處理字元或者字元串。可以這樣說,位元組流呢,提供了最基本IO功能,可以處理任何IO操作,但是有一點,不可以直接處理Unicode字元,為什麼呢,因為Unicode字元是一個單元為2個位元組的字元,而位元組流的處理單元為1個位元組,字元流是由Java虛擬機將位元組轉化為2個位元組的Unicode字元為單位的字元形成的。如果使用位元組流直接處理Unicode字元的話,由於各種編碼實現的不同,就很容易出現亂碼。
1.3實例上,我們日常處理的很多數據,都是文本,所以我們在位元組流之外呢,又提出了字元流的概念,直接按照Java虛擬機的encode來處理,也就是在輸出流和輸入流之間 進行字元集的轉換。
2.java字元流實現結構
1.輸入字元流
2.輸出字元流
3.字元流常用類
3.1 字元數組輸入輸出流(CharArrayReader和CharArrayWriter)
package se.io; import com.sun.org.apache.xpath.internal.operations.String; import java.io.CharArrayReader; import java.io.CharArrayWriter; import java.io.IOException; public class CharArrayTest { public static void main(String[] args) { char[] chars = new char[3]; chars[0] = 100; chars[1] = 101; chars[2] = 102; CharArrayReader charArrayReader = new CharArrayReader(chars); try { char[] buf = new char[1024]; charArrayReader.read(buf); CharArrayWriter charArrayWriter = new CharArrayWriter(); charArrayWriter.write(buf); System.out.println(charArrayWriter.toString()); charArrayWriter.close(); charArrayReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.2文件輸入輸出流(FileWriter和FileReader)
package se.io; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileCharTest { public static void main(String[] args) { try { FileReader fileReader = new FileReader("E:\\test\\data.txt"); char[] chars = new char[1024]; int i = fileReader.read(chars); FileWriter fileWriter = new FileWriter("E:\\test\\filewriter.txt"); fileWriter.write(chars,0,i); fileWriter.close(); fileReader.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
3.3管道輸入輸出流(PipedReader 和 PipedWriter)
package se.io; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; public class PipedChar { public static void main(String[] args) { SenderChar senderChar = new SenderChar(); PipedWriter pipedWriter = senderChar.getWriter(); ReceiverChar receiverChar = new ReceiverChar(); PipedReader pipedReader =receiverChar.getReader(); try { pipedWriter.connect(pipedReader); senderChar.start(); receiverChar.start(); } catch (IOException e) { e.printStackTrace(); } } } //創建發送者類 class SenderChar extends Thread{ private PipedWriter writer = new PipedWriter(); public PipedWriter getWriter() { return writer; } public void run(){ String s = new String("hello,world"); try { writer.write(s); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } //創建接受者 class ReceiverChar extends Thread{ private PipedReader reader = new PipedReader(); public PipedReader getReader() { return reader; } public void run(){ String s= null; char[] chars = new char[1024]; try { int i = reader.read(chars); s = new String(chars,0,i); System.out.println(s); reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.4緩存輸入輸出流(BufferedReader 和BufferedWriter)
package se.io; import java.io.*; public class BufferWriterTest { public static void main(String[] args) { try { //構建文件輸入輸出字元流,如果指定文件不存在,則自動生成。 FileReader fileReader = new FileReader("E:\\test\\data.txt"); FileWriter fileWriter = new FileWriter("E:\\test\\data2.txt"); //構建過濾緩存流 BufferedReader bufferedReader = new BufferedReader(fileReader); BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); //緩存數組 char[] chars = new char[1024]; int offset = 0 ; //讀取字元 while(bufferedReader.ready()) { offset = bufferedReader.read(chars); } //輸出字元 bufferedWriter.write(chars,0,offset); //關閉流 bufferedWriter.close(); bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.5格式化輸出流(PrinterWriter)
package se.io; import java.io.FileWriter; import java.io.FilterWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; public class PrintWriterTest { public static void main(String[] args) { try { //創建文件輸出流 FileWriter fileWriter = new FileWriter("E:\\test\\data3.txt"); //創建格式化對象輸出流 PrintWriter printWriter = new PrintWriter(fileWriter); //寫入數據 printWriter.printf("%1$tY年%1$tm月%1$td日", new Date()); printWriter.print(123); printWriter.print(12.32); //關閉流 printWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }