讀寫檔案編碼問題

2021-09-02 14:00:04 字數 1195 閱讀 3699

filewriter和filereader確實使用系統當前預設的編碼方式;

filewriter和filereader都不支援通過引數指定編碼方式,而outputstreamwriter和inputstreamreader可以。這兩個類從名字上就可以看到是位元組流和字元流的組合,實際上也是連線兩者的橋梁

//writing to a file , encoding is utf-8

public static void writetofileutf8(string str, string filename) throws ioexception

bufferedwriter writer=new bufferedwriter(new outputstreamwriter(new fileoutputstream(f),"utf-8"));

writer.write(str);

writer.close();

}file f = new file(filename);

if (!f.exists())

bufferedwriter writer=new bufferedwriter(new outputstreamwriter(new fileoutputstream(f, true),"utf-8"));

writer.write(str);

writer.close();

} private static mapreadfile(string filename) throws exception

if(sb != null)

reader.close();

} catch (ioexception e) finally catch (ioexception e1) }}

return contentmap;

}//得到檔案的編碼格式 utf-8時,有bom的檔案前3個位元組為 ef bb bf

private static string getfilecharacterencode(string filename) catch (ioexception e)

string code = "";

switch (p)

log.info("file encode is : " + code);

return code;

}

New IO 讀寫檔案,編碼出現問題

最近在做乙個fileutil,技術採用new io,在做的時候出現了編碼問題 例如 我採用writefile d test.txt 中國 null 然後我用readfile d test.txt 讀結果就會返回亂碼 後來我用charset解碼 獲取目標檔案編碼 system.getproperty ...

Python 檔案讀寫,中文編碼

寫入檔案 file open e baidunetdiskdownload 基礎 day11 1.txt w r讀,w寫 覆蓋 wb以二進位制寫,a寫 不覆蓋 file open r e baidunetdiskdownload 基礎 day11 1.txt w mystr yingwen file...

檔案讀寫問題

檔案讀寫問題 delphi windows sdk api 我的檔案是這樣的 配置 00 01 你好 00 03 歡迎 00 14 光臨 01 20 ok 01 23 bye 配置1 00 02 你好1 00 033 歡迎1 00 50 光臨1 01 22 ok1 01 26 bye 1 其中 00...