1. 程式人生 > >021.4 IO流——字節、字符橋梁(編碼解碼)

021.4 IO流——字節、字符橋梁(編碼解碼)

sta color cep println utf8 讀取 txt leo row

默認使用的就是gbk編碼,這裏的例子改成了utf8編碼

寫入—編碼

private static void writeText() throws IOException
{
    FileOutputStream fos = new FileOutputStream("utf8.txt");
    OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
    osw.write("求");
    osw.close();
}

讀取—解碼

private static void readCNText() throws
IOException { FileInputStream fis = new FileInputStream("utf8.txt"); InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); int i = 0; while((i = isr.read())!=-1){ System.out.println((char)i); } isr.close(); }

字符流 = 字節流 + 編碼表


#####################快捷操作的類
FileWriter and FileReader

021.4 IO流——字節、字符橋梁(編碼解碼)