1. 程式人生 > >java 核心編程——IO流之字符流(三)

java 核心編程——IO流之字符流(三)

sun logs printf file start ech read private per

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();
        }
    }

}

java 核心編程——IO流之字符流(三)