感谢任何帮助,我正在努力学习java并进行练习。我试图读取由管道字符分隔的文本文件,将该数据存储到数组或类中以便对其进行排序,然后最终将此已排序的数据打印回另一个文本文件。
示例输入文件:
年龄| FirstName | MiddleName |市
Age2 | FirstName2 | MiddleName2 |城2
......
等
然后我想按年龄排序,最早的。如果此列表中的任何人年龄相同,我希望按名字的字母顺序对它们进行排序。
最后,我想将这个新的排序数据写入另一个文本文件,例如:
年龄
FirstName,LastName
“地点:”城市
AGE2岁
FirstName2,LastName2
“地点:”City2
我有点迷失在哪里开始。我开始将文件读入数组,但后来不确定如何将数据保存在一起。我想我正在寻求如何最好地解决这个问题的帮助。这是我开始的方式......
import java.io.BufferedReader;
import java.io.FileReader;
public class split2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split("\\|");
for (String str : values) {
System.out.println(str);
}
}
br.close();
}}
然后我试图使用这样的逻辑将其分解为自己的数据类型但是遇到了问题,因为我不确定如何通过“|”解析这里有分隔符:
try {
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
while (true) {
final String line = br.readLine();
if (line == null) break;
age = Integer.parseInt(br.readLine());
fname = br.readLine();
lname = br.readLine();
city = br.readLine();
System.out.println(age + "\t" + fname + "\t" + lname + "\t" + city);
}
非常感谢任何帮助。谢谢。
答案 0 :(得分:1)
您需要更改while循环的代码块,如下所示 -
while (true) {
final String line = br.readLine();
if (line == null) break;
String []data = line.split("\\|");
age = Integer.parseInt(data[0]);
fname = data[1];
lname = data[2];
city = data[3];
System.out.println(age + "\t" + fname + "\t" + lname + "\t" + city);
}
假设文件中的所有行包含带有分隔符|
答案 1 :(得分:1)
当您需要在内存中对数据进行分组时,类是伟大的方法。您可以轻松拥有一个Person
bean来保存所有数据。
class Person implements Comparable<Person> {
private int age;
private String firstName;
private String lastName;
private String city;
public Person(int age, String firstName, String lastName, String city) {
this.age = age;
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return age + System.lineSeparator() +
firstName + ", " + lastName + System.lineSeparator() +
"Location: " + city;
}
@Override
public int compareTo(Person person) {
int result = this.age - person.getAge();
if (result == 0) {
result = this.firstName.compareTo(person.getLastName());
}
return result;
}
}
然后你的阅读和写作看起来像
List<Person> people = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
String line;
while ((line = br.readLine()) != null) {
String[] parts = line.split(" \\| ");
people.add(new Person(Integer.parseInt(parts[0]), parts[1], parts[2], parts[3]));
}
} catch (IOException e) {
System.err.println("Error reading file");
}
Collections.sort(people);
try (BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"))) {
for (int i = 0; i < people.size(); i++) {
if (i > 0) {
bw.write(System.lineSeparator());
bw.write(System.lineSeparator());
}
bw.write(people.get(i).toString());
}
} catch (IOException e) {
System.err.println("Error writing file");
}
如果您不介意尾随空格,则可以在循环遍历Person
对象时使用增强型for循环。
答案 2 :(得分:0)
一些变化:
import java.io.BufferedReader;
import java.io.FileReader;
public class split2 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("data.txt"));
String line = null;
while ((line = br.readLine()) != null) {
String[] values = line.split("\\|");
if (values.length<4) continue; // problem !!!
String age=values[0];
String fname=values[1];
String lname=values[2];
String city=values[3];
System.out.println(age + "\t" + fname + "\t" + lname + "\t" + city);
}
br.close();
}}
答案 3 :(得分:-4)
您的代码问题: -
以下是您的完整解决方案,只需在您的计算机上复制并运行
即可public class PipedFile {
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new FileReader("C:/inputPiped.txt"));
ArrayList<Person> list = new ArrayList<Person>();
Person p = null;
String readLine = br.readLine();
while(readLine != null){
String [] person = readLine.split("\\|");
System.out.println(person.length);
p = new Person();
p.setAge(Integer.parseInt(person[0]));
p.setFname(person[1]);
p.setLname(person[2]);
p.setCity(person[3]);
list.add(p);
readLine = br.readLine();
}
Collections.sort(list);
FileOutputStream fout = new FileOutputStream("C:/ooo.txt");
for(Person prsn : list){
fout.write(prsn.toString().getBytes());
fout.write('\n');
}
System.out.println("DONE");
}
}
class Person implements Comparable<Person>{
int age;
String fname;
String lname;
String city;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public int compareTo(Person p) {
if(this.age < p.age){
return 1;
}else if(this.age > p.age){
return -1;
}else{
return this.fname.compareTo(p.fname);
}
}
@Override
public String toString() {
return this.age + " | " + this.fname + " | " + this.lname + " | " + this.city ;
}
}