我有两个文件 Persons.txt 和 Hobby.txt。在第三个文件中我想把所有的人的名字和每个名字添加一些爱好。这就是我的文件看起来像 Persons.txt
ID;NUME;PRENUME;DATA_NASTERII;PROFESIA
1;Stan;Ilie;22-01-1977;profesor;
2;Becali;GG;01-07-1965;patron;
3;Tanase;Cristian;07-12-1988;fotbalist;
4;Pop;Ion;21-03-1984;pictor;
5;Popescu;Rodica;17-04-1986;sculptor;
爱好.txt
ID;NUME;DESCRIERE;NUMAR_MINIM_PERSOANE;ELEMENT_NECESAR
1;baschet;sport in care se arunca mingea la cos;6;minge
2;fotbal;sport in care nue voie sa atingi mingea in poarta adversa;14;minge
3;chitara;cantatul la chitara;1;chitara
4;pianul; cantatul la pian;1;pian
5;programarea;scrierea de programe software;1;PC
我需要第三个文件,如下所示:
Ion Pop : baschet, volei
Ilie Stan: steaua, handbal
问题是我不知道
- 如何获取人名和
- 如何给他们添加 2 或 3 个爱好。
- 如何从文件中拆分每一行并写入第三个文件
我的代码
import java.io.*;
import java.util.*;
import java.util.ArrayList;
public class ListaHobby {
String line="";
Persoana p = new Persoana();
Hobby h = new Hobby();
public void writeListaHobbies(){
try{
FileReader file1 =new FileReader("Persoane.txt");
Scanner scan = new Scanner(new File("Persoane.txt"));
ArrayList<String> values = new ArrayList<String>();
System.out.println(values.size());
FileReader file2 = new FileReader("Hobby.txt");
scan = new Scanner(new File("Hobby.txt"));
values = new ArrayList<String>();
System.out.println(values.size());
while(scan.hasNext()){
values.add(scan.next());
}
BufferedReader br1 = new BufferedReader(file1);
BufferedReader br2 = new BufferedReader(file2);
String temp1="";
String temp2="";
while(br1.readLine() != null){
temp1 = br1.readLine() + temp1;
}
while(br2.readLine() != null){
temp2 = br2.readLine() + temp2;
}
String temp = temp1 + temp2;
FileWriter fw = new FileWriter("PersHobby.txt");
char buffer[] = new char[temp.length()];
temp.getChars(0, temp.length(), buffer, 0);
fw.write(buffer);
file1.close();
file2.close();
fw.close();
}
catch(IOException ex){
System.out.println("Error opening file.");
System.exit(1);
}`