我嘗試讀取文件到字符串,我試圖使編碼爲UTF-8,但仍然失敗,它會返回輸出中的一些奇怪的字符。奇怪的字符當從文本中讀取srt文件
這裏是我的功能來讀取文件:
private static String readFile(String path, boolean isRaw) throws UnsupportedEncodingException, FileNotFoundException{
File fileDir = new File(path);
try{
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(fileDir), "UTF-8"));
String str;
while ((str = in.readLine()) != null) {
System.out.println(str);
}
in.close();
return str;
}
catch (UnsupportedEncodingException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
return null;
}
第一線的輸出是:1
在此先感謝。
您的CLI可能不支持Unicode字符集,可能會產生 –
如果只有1個文件,在'記事本打開它++'並將它轉換成'UTF-8'並處理它,你正在做的 – Valijon