简体   繁体   English

Java程序读取文本文件

[英]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. 使用String.substring(start, end)来获取该行的相关部分。
  • Call trim() on it. 调用trim()就可以了。
  • Read the file line by line 逐行读取文件
  • call String.split to split the different informations into an array 调用String.split将不同的信息拆分成一个数组
  • 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 您可以将这些信息存储在一个Map该键的关键字是用户名vinoth ,值将是您刚从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]);

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

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