简体   繁体   English

在 Java 中是否有任何方法可以从文件中读取一行数据?

[英]In Java is there any method to read a data of a line from file?

Is there a method to read the first and the last data from a line which are separated by space from a file in java.有没有一种方法可以从 java 中的文件中读取由空格分隔的行中的第一个和最后一个数据。 Example:例子:
the file contains the following information该文件包含以下信息

100 20-11-2020 08:25:42 IN
101 21-09-2020 09:01:20 IN

Here I just want 100 and IN to extract and print在这里我只想要 100 和 IN 提取和打印

One approach is to read the entire string and use the split method.一种方法是读取整个字符串并使用 split 方法。 Store the split string in an array and simply access the first and last element., something like this:将拆分字符串存储在一个数组中,然后简单地访问第一个和最后一个元素。,如下所示:

String line = "100 20-11-2020 08:25:42 IN"
String arr[] = line.split(" ");
String var1 = arr[0];//stores 100
String var2 = arr[arr.length - 1];//stores IN

Hope that helps!希望有帮助! Happy coding!快乐编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM