有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用文件处理将数据从一个移动到另一个时出现问题

为了提供一些上下文,我正在用Java编写代码,使用文件处理将数据从一个文件移动到另一个文件。这些文件的命名格式如下:“form+stream.txt”。表格相当于学生的成绩,例如,如果他是12年级,那么他的表格是SC,而流相当于段,可以是1、2、3、4或5。因此,如果一名学生在11年级和5年级,他的数据将存储在名为“S5.txt”的文件中。以下代码显示学生类的所有变量:

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?");
    }
}  
}
}

此代码工作不正常,产生错误结果。星型模式出现在原始文件上,我认为这是个好消息,但是处理向文件写入新数据的最后一段代码不起作用。新的文本文件应该包含信息,但看起来是空白的,带有空格。我犯了什么错误?请帮忙

PS:很抱歉问题太长,如果需要更多关于代码的信息,请告诉我

多谢各位


共 (0) 个答案