Ron*_*gan 1 java string file-io
我想String使用分隔符","在文件中提取第一个.为什么此代码生成大于一行的行数?
public static void main(String[] args) {
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader("irisAfter.txt"));
String read = null;
while ((read = in.readLine()) != null) {
read = in.readLine();
String[] splited = read.split(",");
for (int i =0; i<splited.length;i++) {
System.out.println(splited[0]);
}
}
} catch (IOException e) {
System.out.println("There was a problem: " + e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) { e.printStackTrace(); }
}
}
Run Code Online (Sandbox Code Playgroud)
您正在循环内打印.这就是它多次打印的原因(如果这就是你所要求的).
String[] splited = read.split(",");
System.out.println(splited[0]);
Run Code Online (Sandbox Code Playgroud)
会做的
编辑:正如Abishek也提到的那样,不要read = in.readLine();再在你的while循环中了,因为你这样做是在跳过一条线.
while ((read = in.readLine()) != null) {
String[] splited = read.split(",");
System.out.println(splited[0]);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
228 次 |
| 最近记录: |