[英]reading in delimited line from txt file in java
感謝您的幫助,我正在嘗試學習Java並進行一項練習。 我正在嘗試讀取由豎線字符分隔的文本文件,將該數據存儲到數組或類中以便對其進行排序,然后最終將此排序后的數據打印回另一個文本文件。
輸入文件示例:
年齡| 名| MiddleName | 市
Age2 | FirstName2 | MiddleName2 | 城市2
......等等
然后,我想按年齡(最早的)排序。 如果此列表中有相同年齡的人,我想按名字的字母順序對他們進行排序。
最后,我想將此新排序的數據寫入另一個文本文件,例如:
年齡
名,姓
“位置:”城市
年齡2
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);
}
任何幫助將非常感激。 謝謝。
您需要像下面這樣更改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);
}
假設文件中的所有行包含帶有定界符的4個字段|
當您需要對內存中的數據進行分組時,使用類是一種很好的方法。 您可以輕松地使用一個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循環。
一些變化:
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();
}}
您的代碼有問題:-
這是您的完整解決方案 ,只需將其復制並在您的計算機上運行,
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 ;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.