在Java中使用文件处理将数据从一个移动到另一个时出现问题
为了提供一些上下文,我正在用Java编写代码,使用文件处理将数据从一个文件移动到另一个文件。这些文件的命名格式如下:“form+stream.txt”。表格相当于学生的成绩,例如,如果他是12年级,那么他的表格是SC,而流相当于段,可以是1、2、3、4或5。因此,如果一名学生在11年级和5年级,他的数据将存储在名为“S5.txt”的文件中。以下代码显示学生类的所有变量:在Java中使用文件处理将数据从一个移动到另一个时出现问题,java,file,Java,File,为了提供一些上下文,我正在用Java编写代码,使用文件处理将数据从一个文件移动到另一个文件。这些文件的命名格式如下:“form+stream.txt”。表格相当于学生的成绩,例如,如果他是12年级,那么他的表格是SC,而流相当于段,可以是1、2、3、4或5。因此,如果一名学生在11年级和5年级,他的数据将存储在名为“S5.txt”的文件中。以下代码显示学生类的所有变量: class Student { public int school_number; String surname; St
class Student
{
public int school_number;
String surname;
String first_name;
String current_board;
String form;
int stream;
String house;
int late_mark;
int absent_mark;
int present_mark;
//methods of Student class
}
此信息以以下格式存储在文本文件中:
学号
名字
姓
当前板
形式
溪流
房子
晚点
缺席分数
当前标记
现在假设我想更改任何特定学生的形式,我可以通过创建另一个文本文件轻松做到这一点。但是,本例中的问题是,通过更改学生的表单,我必须将他移动到另一个文件中,因为如果您还记得,我提到,学生是根据他们的流/节和表单添加到文件中的。因此,我必须首先从现有文件中删除该学生,并将其添加到一个基于其表单和流的新组合的文件中。为此,我使用了以下代码:
System.out.println("Welcome");
System.out.println();
System.out.println("Please enter the School Number of the Student whose Information you would like to change");//Since school number is Unique
int x=Integer.parseInt(bw.readLine());
int test=0;
for(int i=0;i<30;i++)
for(int j=0;j<100;j++)
{ if(a[i][j]!=null)
// This array is reads all the student information from the text files and creates objects of student type.
{
if(this.a[i][j].school_number==x)
{
System.out.println("The Previous Entry of the Form was: "+this.a[i][j].form);
System.out.println("Please enter the new Form");
String w=this.bw.readLine();
Student q=new Student();
String num="";
num=num.valueOf(x);
if(w.equals("SC")||w.equals("S")||w.equals("A")||w.equals("B")||w.equals("C")||w.equals("D")) //Checking whether the input is valid or not.
{
//Making Changes to the File
File f=new File("Temp.txt");
f.createNewFile();
String v="";
v=v.valueOf(this.a[i][j].stream);
String u=this.a[i][j].form;
BufferedReader br=new BufferedReader(new FileReader(u+v+".txt")); //Form+Stream as I mentioned earlier
BufferedWriter bw=new BufferedWriter(new FileWriter("Temp.txt"));
String a=br.readLine();
while(a!=null)//Writing Data to Temp File
{ bw.write(a);
bw.newLine();
a=br.readLine();
}
br.close();
bw.close();
BufferedReader br1=new BufferedReader(new FileReader("Temp.txt"));
BufferedWriter bw1=new BufferedWriter(new FileWriter(u+v+".txt"));
String b=br1.readLine();
while(b!=null)
{ if(b.equals(num))// Deleting the student from the previous file
{ bw1.write("*"); //I didn't know what else to write
bw1.newLine();
String z1=br1.readLine();
if(z1.equals(this.a[i][j].first_name))
bw1.write("*");
bw1.newLine();
String z2=br1.readLine();
if(z2.equals(this.a[i][j].surname))
bw1.write("*");
bw1.newLine();
String z3=br1.readLine();
if(z3.equals(this.a[i][j].current_board))
bw1.write("*");
bw1.newLine();
String z4=br1.readLine();
if(z4.equals(this.a[i][j].form))
bw1.write("*");
bw1.newLine();
String z5=br1.readLine();
int q0=Integer.parseInt(z5);
if(q0==this.a[i][j].stream)
bw1.write("*");
bw1.newLine();
String z6=br1.readLine();
if(z6.equals(this.a[i][j].house))
bw1.write("*");
bw1.newLine();
String z7=br1.readLine();
int q1=Integer.parseInt(z7);
if(q1==this.a[i][j].late_mark)
bw1.write("*");
bw1.newLine();
String z8=br1.readLine();
int q2=Integer.parseInt(z8);
if(q2==this.a[i][j].absent_mark)
bw1.write("*");
bw1.newLine();
String z9=br1.readLine();
int q3=Integer.parseInt(z9);
if(q3==this.a[i][j].present_mark)
bw1.write("*");
}
else
bw1.write(b);
bw1.newLine();
b=br1.readLine();
}
bw1.close();
br1.close();
BufferedReader br2=new BufferedReader(new FileReader("Temp.txt"));
String g="";
g=g+w;
int h=this.a[i][j].stream;
String hh="";
hh=hh.valueOf(h);
String new_file=g+hh;
BufferedWriter bw2=new BufferedWriter(new FileWriter(new_file+".txt",true));
String bb=br2.readLine();
while(bb!=null&&!(bb.equals("")))//Adding data to the new File.
{ if(bb.equals(num))
{bw2.newLine();
bw2.write(bb);
bw2.newLine();
String z1=br2.readLine();
if(z1.equals(this.a[i][j].first_name))
bw2.write(z1);
bw2.newLine();
String z2=br2.readLine();
if(z2.equals(this.a[i][j].surname))
bw2.write(z2);
bw2.newLine();
String z3=br2.readLine();
if(z3.equals(this.a[i][j].current_board))
bw2.write(z3);
bw2.newLine();
String z4=br2.readLine();
if(z4.equals(this.a[i][j].form))
bw2.write(w);
bw2.newLine();
String z5=br2.readLine();
int q0=Integer.parseInt(z5);
if(q0==this.a[i][j].stream)
bw2.write(z5);
bw2.newLine();
String z6=br2.readLine();
if(z6.equals(this.a[i][j].house))
bw2.write(z6);
bw2.newLine();
String z7=br2.readLine();
int q1=Integer.parseInt(z7);
if(q1==this.a[i][j].late_mark)
bw2.write(z7);
bw2.newLine();
String z8=br2.readLine();
int q2=Integer.parseInt(z8);
if(q2==this.a[i][j].absent_mark)
bw2.write(z8);
bw2.newLine();
String z9=br2.readLine();
int q3=Integer.parseInt(z9);
if(q3==this.a[i][j].present_mark)
bw2.write(z9);
bb=br2.readLine();
}
else
bb=br2.readLine();
}
bw2.close();
br2.close();
f.delete();
test=1;
}
}
}
}
if(test==1)
System.out.println("Change Successful!");
if(test==0)
{
System.out.println("Student not found in DATABASE. Would you like to add student?");
}
}
}
}
System.out.println(“欢迎”);
System.out.println();
System.out.println(“请输入您想要更改其信息的学生的学号”)//因为学校编号是唯一的
intx=Integer.parseInt(bw.readLine());
int检验=0;
对于(int i=0;iIs从txt文件移动到DB选项?如果第三方系统需要更改,您可以在发生更改时从DB转储txt文件。其他应用程序是否会更改这些文件?@Fildor,感谢您的回复。老实说,我还是一个初学者,不知道DB代表什么。请详细说明您的观点?DB=Data我的意思是一个真正的数据库,比如MySQL、SQLite,或者如果你喜欢一些NoSQL数据库……将所有数据保存在txt文件中真的是……好吧,你已经遇到了问题。至少你应该使用一些更好的(和众所周知的)格式化,以便您可以轻松地序列化/反序列化为对象。如XML或JSON。是否可以将txt文件移动到DB?如果第三方系统需要,您可以在发生更改时从DB转储txt文件。其他应用程序是否会更改这些文件?@Fildor,感谢您的回复。老实说,我还是一个初学者,不知道DB代表什么。你能详细说明一下你的观点吗?DB=Database。我的意思是像MySQL、SQLite这样的真正的数据库,或者如果你喜欢一些NoSQL DB…将所有数据保存在txt文件中真的是…好吧,你已经遇到了问题。至少你应该使用一些更好的(并且众所周知的)数据库格式,以便您可以轻松地序列化/反序列化为对象。如XML或JSON。