【发布时间】:2013-01-16 06:25:20
【问题描述】:
我有一个csv file,其中包含英语单词和印地语翻译。我正在尝试读取 csv 文件并对其进行进一步处理。 csv 文件如下所示:
English,,Hindi,,,
,,,,,
Cat,,बिल्ली,,,
Rat,,चूहा,,,
abandon,,छोड़ देना,त्याग देना,लापरवाही की स्वतन्त्रता,जाने देना
我正在尝试逐行读取 csv 文件并显示已写入的内容。代码sn -p(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 的帖子,但我需要更多帮助来找出这个问题的确切原因。
【问题讨论】:
-
旁注:您不必像
if(line.contains("English") == true)和(startSeen == true)那样将布尔值等同起来,而是可以直接使用if(line.contains("English"))和(startSeen),因为它们可能是 true 或 false . -
@smit:点了。谢谢!