1. 程式人生 > >JAVA-轉換流

JAVA-轉換流

InputStreamReader

檢視API文件,發現是位元組流通向字元流的橋樑。檢視構造,可以傳遞位元組流,可以指定編
工作。該流是一個Reader的子類,是字元流的體系。所以將轉換流稱之為位元組流和字元流
之間的橋樑。
InputStreamReader 是位元組流通向字元流的橋樑
測試InputStreamReader:
    第一步: 需要專門新建以GBK編碼的文字檔案。為了便於標識,我們命名為gbk.txt
        和以UFT-8編碼的文字檔案,命名為utf.txt
    第二步: 分別寫入漢字”中國”
    第三步:編寫測試方法,用InputStreamReader 分別使用系統預設編碼,GBK,
    UTF-8編碼讀取檔案.
public class Demo1 {

    public static void main(String[] args) throws Exception {
        //系統預設編碼檔案
        File file = new File("./src/a.txt");
        //使用gbk編碼的檔案
        File gbkFile = new File("./src/gbk.txt");
        //使用utf-8編碼的檔案
        File utfFile = new File("./src/utf.txt");

        // 預設編碼
        testReadFile(gbkFile);
        System.out
.println("===================="); // 傳入gbk編碼檔案,使用gbk解碼 testReadFile(utfFile, "gbk"); System.out.println("===================="); // 傳入utf-8檔案,使用utf-8解碼 testReadFile(file, "utf-8"); } private static void testReadFile(File file, String string) throws Exception { FileInputStream fis = new
FileInputStream(file); InputStreamReader reader = new InputStreamReader(fis,string); int content = 0; while((content = reader.read())!=-1){ System.out.print((char)content); } reader.close(); System.out.println(""); } private static void testReadFile(File file) throws Exception { FileInputStream fis = new FileInputStream(file); InputStreamReader reader = new InputStreamReader(fis); int content = 0; while((content = reader.read())!= -1){ System.out.print((char)content); } reader.close(); System.out.println(""); } }

OutputStreamWriter

有了InputStreamReader 可以轉換InputStream  
那麼其實還有OutputStreamWriter 可以轉換OutputStream
OutputStreamWriter 是字元流通向位元組流的橋樑
測試OutputStreamWriter 
    一: 分別使用OutputStreamWriter使用系統預設編碼,GBK,UTF-8相對應的預設
    編碼檔案,GBK編碼檔案,UTF-8編碼檔案中寫出漢字”中國”.
    二: 在使用上述案例中的readFile方法傳入相對應碼錶讀取.
public class TestIo {
    public class Demo4 {
    public static void main(String[] args) throws IOException {
        File file = new File("c:\\a.txt");
        File fileGBK = new File("c:\\gbk.txt");
        File fileUTF = new File("c:\\utf.txt");

        // 寫入
        // 使用系統預設碼錶寫入
        testWriteFile(file);
        // 使用gbk編碼向gbk檔案寫入資訊
        testWriteFile(fileGBK, "gbk");
        // 使用utf-8向utf-8檔案中寫入資訊
        testWriteFile(fileUTF, "utf-8");

        // 讀取
        // 預設編碼
        testReadFile(file);
        // 傳入gbk編碼檔案,使用gbk解碼
        testReadFile(fileGBK, "gbk");
        // 傳入utf-8檔案,使用utf-8解碼
        testReadFile(fileUTF, "utf-8");

    }

    // 使用系統碼錶將資訊寫入到檔案中
    private static void testWriteFile(File file) throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter ops = new OutputStreamWriter(fos);
        ops.write("中國");
        ops.close();
    }

    // 使用指定碼錶,將資訊寫入到檔案中
    private static void testWriteFile(File file, String encod)
            throws IOException {
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter ops = new OutputStreamWriter(fos, encod);
        ops.write("中國");
        ops.close();
    }

    // 該方法中nputStreamReader使用系統預設編碼讀取檔案.
    private static void testReadFile(File file) throws IOException {
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader ins = new InputStreamReader(fis);
        int len = 0;
        while ((len = ins.read()) != -1) {
            System.out.print((char) len);
        }
        ins.close();

    }

    // 該方法適合用指定編碼讀取檔案
    private static void testReadFile(File file, String encod)
            throws IOException {
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader ins = new InputStreamReader(fis, encod);
        int len = 0;
        while ((len = ins.read()) != -1) {
            System.out.print((char) len);
        }

        ins.close();
    }

}