我有一個csv file
其中包含英語單詞,其後是印地語翻譯。我正在嘗試讀取csv文件並使用它進行一些進一步的處理。 CSV文件看起來像這樣:從csv文件讀取unicode字符
English,,Hindi,,,
,,,,,
Cat,,बिल्ली,,,
Rat,,चूहा,,,
abandon,,छोड़ देना,त्याग देना,लापरवाही की स्वतन्त्रता,जाने देना
我想逐行讀取CSV文件行,並顯示已被寫入。代碼段(Java
)如下:
//Step 2. Read csv file and get the string.
FileInputStream fis = null;
BufferedReader br = null;
try {
fis = new FileInputStream(new File(csvFile));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
boolean startSeen = true;
if(fis != null) {
try {
br = new BufferedReader(new InputStreamReader(fis, "UTF-8"));
} catch (UnsupportedEncodingException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
System.out.print("Unsupported encoding");
}
String line = null;
if(br != null) {
try {
while((line = br.readLine()) != null) {
if(line.contains("English") == true) {
startSeen = true;
}
if((startSeen == true) && (line != null)) {
StringBuffer sbuf = new StringBuffer();
//Step 3. Parse the line.
sbuf.append(line);
System.out.println(sbuf.toString());
}
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
但是,下面的輸出就是我得到:
English,,Hindi,,,
,,,,,
Cat,,??????,,,
Rat,,????,,,
abandon,,???? ????,????? ????,???????? ?? ???????????,???? ????
我的Java是不是偉大的,雖然我已經通過一些崗位的消失在SO上,我需要更多的幫助來弄清楚這個問題的確切原因。
只是一側評論:你不必像你正在做的'如果等同於布爾值(線.contains(「English」)== true)'和'(startSeen == true)',您可以直接使用if(line.contains(「English」))和'(startSeen)',因爲它們可能是對或錯。 – Smit
@smit:採取的點。謝謝! – Sriram