如何解决如何在java中提取文本文件的键和值?
我找不到在java中提取像下面这样的数据的方法我也使用正则表达式来分离同一行中的数据,但我无法解决这个问题,这仅供练习 所以请如果有人可以帮助我!请
示例文本文件:-
school highschool name delhischool
address delhi street metros-station
school SecondarySchool name metroSchool
address mombie street bus-station
例如:学校:高中 名称:德里学校 类似其他
示例代码:-
File myObj = new File("C:\\Users\\text1.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data;
// while (myReader.nextLine().contains("----"))
data = myReader.nextLine();
if (data.contains("name")) {
List<String> split = Arrays.asList(data.split("\\s{3}+"));
// String[] split = data.split("\\s{3,1000}+");
System.out.println(split);
// System.out.println(data);
解决方法
我认为最好的方法是创建一个类来支持您要读入的文件数据。这是一个名为 SchoolData 的示例类:
public class SchoolData {
private String school;
private String name;
private String address;
private String street;
// Constructor 1
public SchoolData() { }
// Constructor 2
public SchoolData(String school,String name,String address,String street) {
this.school = school;
this.name = name;
this.address = address;
this.street = street;
}
public String getSchool() {
return school;
}
public void setSchool(String school) {
this.school = school;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
@Override
public String toString() {
return new StringBuilder(this.school).append(",").append(this.name).append(",")
.append(this.address).append(",").append(this.street).toString();
}
}
要读取数据文件,您可以尝试这样的操作(有很多方法可以实现):
File myObj = new File("SchoolData.txt");
List<SchoolData> dataList = new ArrayList<>();
// 'Try With Resources' used here to auto-close reader.
try (Scanner myReader = new Scanner(myObj)) {
String data;
while (myReader.hasNextLine()) {
data = myReader.nextLine().trim(); // Get fist line of record data
// If the line isn't blank and it starts with the word 'school' then
// it must be the start of a new data record.
if (!data.isEmpty() && data.toLowerCase().startsWith("school")) {
String[] lineParts = data.split("\\s+");
String school = lineParts[0] + ":" + lineParts[1];
String name = lineParts[2] + ":" + lineParts[3];
data = myReader.nextLine().trim(); // Get second line of record data
lineParts = data.split("\\s+");
String address = lineParts[0] + ":" + lineParts[1];
String street = lineParts[2] + ":" + lineParts[3];
// Create a new instance of SchoolData which will be held
// within the dataList List Interface to access as you like.
dataList.add(new SchoolData(school,name,address,street));
}
}
}
catch (FileNotFoundException ex) {
System.err.println(ex);
}
System.out.println("Display all instances of SchoolData:");
System.out.println();
for (int i = 0; i < dataList.size(); i++) {
System.out.println(dataList.get(i).getSchool());
System.out.println(dataList.get(i).getName());
System.out.println(dataList.get(i).getAddress());
System.out.println(dataList.get(i).getStreet());
System.out.println("===============================");
System.out.println();
}
System.out.println("Another way to display all instances of SchoolData");
System.out.println("using the SchoolData#toString() method:");
System.out.println();
for (int i = 0; i < dataList.size(); i++) {
System.out.println(dataList.get(i).toString());
}
如果一切顺利并且使用您提供的文件数据,您的控制台窗口应该显示如下内容:
Display all instances of SchoolData:
school:highschool
name:delhischool
address:delhi
street:metros-station
===============================
school:SecondarySchool
name:metroSchool
address:mombie
street:bus-station
===============================
Another way to display all instances of SchoolData
using the SchoolData#toString() method:
school:highschool,name:delhischool,address:delhi,street:metros-station
school:SecondarySchool,name:metroSchool,address:mombie,street:bus-station
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。