解決IO流讀取檔案亂碼問題

2021-07-26 16:26:30 字數 1056 閱讀 2880

讀取檔案時,出現亂碼,檔案是txt格式,編碼格式預設為ansi格式,即gbk

於是轉為utf-8

inputstreamreader reader = new inputstreamreader(   new fileinputstream(new file("檔案路徑")), "utf-8");
但是貌似不支援符號,空格等,輸出中文時出現????

於是引用乙個轉碼類,將原始檔解析,轉成utf-8,再讀出。

public static string get_charset(file file)  else if (first3bytes[0] == (byte) 0xfe

&& first3bytes[1] == (byte) 0xff) else if (first3bytes[0] == (byte) 0xef

&& first3bytes[1] == (byte) 0xbb

&& first3bytes[2] == (byte) 0xbf)

bis.reset();

if (!checked) else if (0xe0 <= read && read <= 0xef) else

break;

} else

break;}}

}bis.close();

} catch (exception e)

return charset;

}

然後呼叫轉碼方法輸出:

public static void main(string args) 

if (charset == "utf-8")

string s;

while((s = br.readline()) != null)

system.out.println(s);

string(s.getbytes("utf-8"),"utf-8"));

} catch(exception e)

}

IO檔案讀取以及解決中文亂碼

一 讀取檔案內容的三種方式 1 檔案的讀取 主要介紹兩種常用的讀取方法。按行讀取和按字元塊讀取。1.1 一次讀取一行作為輸入 按行讀取檔案內容 public static string read1 string infile throws exception infile data in.txt b...

解決C 讀取檔案亂碼問題

儲存如下內容到mapconfig.json檔案 讀取時出現亂碼 其中,讀取部分的 如下 jobject jmapconfig jobject.parse system.io.file.readalltext spath jarray aarray jmapconfig basemap as jarr...

io流檔案的讀取

在activity類中定義了openfileinput string name 跟openfileoutput string name,int mode 可以用來進行io流檔案的操作 而且缺省會儲存在記憶體卡當中,下面是 public class fileoperate extends activi...