我正在尝试读取文件并仅打印每行的第一个数字。我曾尝试使用拆分,但它永远不会返回正确的结果,它只是打印整个内容,如下表所示。任何帮助将不胜感激
**thats my file** 40 3 Trottmann 43 3 Brubpacher 252 3 Stalder 255 3 Leuch 258 3 Zeller 261 3 Reolon 264 3 Ehrismann 267 3 Wipf 270 3 Widmer **expected output** 40 43 258 261 264 267 270
输出
258 261 264 267 270
公共课词{
public static void main(String[] args) {
// Create file
File file = new File("/Users/lobsang/documents/start.txt");
try {
// Create a buffered reader
// to read each line from a file.
BufferedReader in = new BufferedReader(new FileReader(file));
String s;
// Read each line from the file and echo it to the screen.
s = in.readLine();
while (s != null) {
System.out.println(s.split("\s")[0]);
s = in.readLine();
}
// Close the buffered reader
in.close();
} catch (FileNotFoundException e1) {
// If this file does not exist
System.err.println("File not found: " + file);
} catch (IOException e2) {
// Catch any other IO exceptions.
e2.printStackTrace();
}
}
}