简体   繁体   中英

Java Program to read text file

I have a text file like

vinoth   5001   chennai    programmer
ramesh   7755   madurai    tester
suresh   7452   namakal    designer
salim    4652   salem      programmer

I want to find with their designation but want to save their other details in an array. how can i do that? any suggestions

  • Use String.substring(start, end) to get the relevant part of the line.
  • Call trim() on it.
  • Read the file line by line
  • call String.split to split the different informations into an array
  • You can store these information in a Map whose key would be the username vinoth and values would be the array you just received from String.split
  • Even better you store these informations in an object that represents something with the properties you'd like.

Read each line using buffered reader and then do that:

HashMap<String, String> values = new HashMap<String, String>;

// read the line here
String line = ...;
String strings[] = line.split(" ");
String designation = strings[3];
values.put(strings[0], strings[1], strings[2]);

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