为了提供一些上下文,我使用Java编写代码,使用文件处理将数据从一个文件移动到另一个文件。文件以以下格式命名:“form + stream.txt”。表格等同于学生的成绩,例如,如果他是12年级,那么他的表格是SC,而Stream相当于部分,可以是1,2,3,4或5.所以如果学生是在11年级和5年级,他的数据将存储在名为“S5.txt”的文件中。以下代码显示了Student Class的所有变量:
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
}
此信息以下列格式存储在文本文件中:
学号
名字
姓
现任董事会
表格
流
楼
Late Mark
缺席马克
现在标记
现在假设我想要更改任何特定学生的表格,我可以通过创建另一个文本文件轻松地做到这一点。然而,在这种情况下的问题是,通过改变学生的形式,我将不得不将他移动到另一个文件,因为如果你记得,我提到学生根据他们的流/部分和表格被添加到文件中。因此,我必须先从现有文件中删除该学生,然后将其添加到一个文件中,该文件基于其表单和流的新组合。为此,我使用了以下代码:
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?");
}
}
}
}
此代码无法正常运行并产生错误结果。星型模式出现在原始文件中,我认为这是个好消息,但是处理将新数据写入文件的最后一段代码无法正常工作。应该有信息的新文本文件看起来是空白的空格。我犯了什么错误?请帮忙
PS:对不起问题的长度,请告诉我是否需要有关代码的更多信息。
谢谢。