我有两个文件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;
Hobby.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
问题在于我不知道
我的代码
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);
}`
答案 0 :(得分:3)
尝试从面向对象的角度来看待它。你必须要对象。一个Person
对象和类似Hobby
对象的东西。文件的每一行代表一个这样的对象。你现在应该做的就是像你一样解析文件,但是用信息创建对象。假设br1
是文件1的读者,它可能如下所示:
while(br1.readLine() != null){
String line = br1.readLine(); // read the line
String[] attributes = line.split(";"); // split it at every ";"
Person person = new Person(); // make a new person
person.setName(attributes[0]);
person.setSurname(attributes[1]);
person.setDate(attributes[2]);
person.setProfession(attributes[3]);
listOfPersons.add(person); // store the person in a list
}
当然,您必须创建一个Person类以及一个Hobby类。
之后,你可以通过你的名单进行迭代,并搜索一个人的爱好。如果您找到了,请将其添加到此人:
for(Person person: listOfPersons) {
for(Hobby hobby: listOfHobbies) {
if(person.getId().equals(hobby.getPerson()))
person.addHobby(hobby);
}
}
之后你会有一份爱好者名单。您可以再次将其写入文件。这种方法需要更多的代码,但大多数都是简单的代码。你以干净的方式做到了。
编辑:
Person类看起来像这样:
公共类人员{
private int id;
private String name;
private String surname;
private String date;
private String profession;
private List<Hobby> hobbies = new ArrayList<Hobby>();
// getter & setter for all this attributes
// here is the one to add hobbies:
public void addHobby(Hobby hobby) {
hobbies.add(hobby);
}
}
答案 1 :(得分:0)
要生成您在问题中定义的文件PersHobby.txt,需要采取以下措施
上述算法非常具有试验性。
由于每个文件的结构相似(分号分隔),我们可能只读取相关字段如下
private static List<String> readFieldFromFile(List<String> values, String delimiter, int index) {
if (values != null && values.size() > 0) {
List<String> returnable = new ArrayList<String>(values.size());
// proceed only if the list of values is non-empty
Iterator<String> it = values.iterator();
// instantiate an iterator
while (it.hasNext()) {
// iterate over each line
String currLine = it.next();
if (currLine != null && currLine.trim().length() > 0) {
// split the line into it's constituent fields if the line is not empty
String[] fields = currLine.split(delimiter);
if (fields.length - index > 0) // Read the particular field, and store it into the returnable
{
returnable.add(fields[index - 1]);
}
}
}
return returnable;
} else {
return null;
}
}
public static List<String> readName(List<String> persons) {
// name occurs at the second position in the line
return readFieldFromFile(persons, ";", 2);
}
public static List<String> readHobby(List<String> hobbies) {
// name of hobby occurs at the second position in the line
return readFieldFromFile(hobbies, ";", 2);
}
接下来,我们需要为一个人的爱好生成一个字符串......类似于下面的方法。这种方法从投掷的爱好中随机抽取两个爱好。
public static String generateRandomHobbies(List<String> hobbies){
String hobbieString = "";
// Read two hobbies at random from the hobbies list into a comma-separated string
if(hobbies.size() >= 2){
Random rand = new Random();
hobbieString.concat( hobbies.get((int)(hobbies.size()*rand.nextFloat())) );
hobbieString.concat( ", " );
hobbieString.concat( hobbies.get((int)(hobbies.size()*rand.nextFloat())) );
hobbieString.concat( ", " );
}
return hobbieString;
}
希望这证明是足够的。我保留字符串的格式,并实际写入输出文件。
P.S。当对象重新初始化时,您使用扫描仪的现有代码有问题。这种重新初始化将使应用程序仅包含Hobby文件的内容。
while(scan.hasNext()){
values.add(scan.next());
}
请解决此问题,以便每个文件中的内容位于单独的列表中。