简体   繁体   中英

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. 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

One approach is to read the entire string and use the split method. 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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