是否可以在close()之后重新打开文件,而无需在同一java程序中再次创建该文件?

时间:2018-12-23 02:02:34

标签: java

为了使程序正常工作,我必须想办法重新打开最初是输出文件的文件。

我的程序需要先读取输入文件,然后再写入输出文件。

然后,我将提示用户在此处输入代码e选项以输入更多代码或从输出文件中搜索任何数据。

我的程序首先选择1插入更多数据,2搜索数据,3退出程序。

我可以在用户输入更多数据后关闭I / O文件,但是如果用户想先搜索数据怎么办?

 if(readFile==0)
            {
                FileReader inFile = new FileReader("DATA4STUDENTS.txt");
                BufferedReader br = new BufferedReader(inFile);

                FileWriter fw1 = new FileWriter("PASSED_STUDENTS.txt");
                BufferedWriter bw1= new BufferedWriter(fw1);//Files i want to write and read again later on in the same program. 

                FileWriter fw2 = new FileWriter("FAILED_STUDENTS.txt");
                BufferedWriter bw2 = new BufferedWriter(fw2);

                PrintWriter pw1 = new PrintWriter(bw1);
                PrintWriter pw2 = new PrintWriter(bw2);
                pw1.print("STUDENT ID\t"+"SUBJECT CODE\t"+"CARRY MARK\t"+"STATUS\t"+"FINAL EXAM\t"+"STATUS\t"+"TOTAL MARK\t"+"STATUS\t"+"GRADE\t"+"GRADE SCORE");
                pw1.println();

                pw2.print("STUDENT ID\t"+"SUBJECT CODE\t"+"CARRY MARK\t"+"STATUS\t"+"FINAL EXAM\t"+"STATUS\t"+"TOTAL MARK\t"+"STATUS\t"+"GRADE\t"+"GRADE SCORE");
                pw2.println();   
                int index=0;
                Assessment [] a = new Assessment[100];
                Assessment [] b = new Assessment[100];
                double[] CM=new double[100]; //array for CarryMark
                double[] FM= new double[100]; //array for FullMark
                boolean[] PG=new boolean[100]; //array for Passing-Grade
                while(((inData=br.readLine()) !=null))
                {
                    StringTokenizer st = new StringTokenizer(inData,"#");
                    StudName=st.nextToken();
                    StudID=st.nextToken();
                    SubName=st.nextToken();
                    FullOn=Integer.parseInt(st.nextToken());
                    FullEX=Integer.parseInt(st.nextToken());
                    mark=Double.parseDouble(st.nextToken());
                    fullM=Integer.parseInt(st.nextToken());
                    EMark=Double.parseDouble(st.nextToken());
                    fullEM=Integer.parseInt(st.nextToken());

                    a[index]=new Ongoing_Assessment(StudName, StudID, SubName,FullOn, FullEX, mark, fullM);
                    b[index]=new Final_Exam_Assessment(StudName, StudID, SubName,FullOn,FullEX,EMark, fullEM);
                    if(a[index] instanceof Ongoing_Assessment)
                    {
                        Ongoing_Assessment OA=(Ongoing_Assessment) a[index];
                        CM[index]=OA.getFinalMark();
                    }
                    if(b[index] instanceof Final_Exam_Assessment)
                    {
                        Final_Exam_Assessment FEA=(Final_Exam_Assessment) b[index];
                        FM[index]=FEA.getFinalMark();
                    }

                    if((CM[index]+FM[index])>=a[index].PassingGrade())
                    {
                        PG[index]=true;
                    }
                    else
                    {
                        PG[index]=false;
                    }
                    index++;
                }

                for(int i=0;i<index;i++)
                {
                    String mss=" ";
                    String mss1=" ";
                    String mss2=" ";
                    String grade=" ";
                    double grade2=0.00;
                    if(PG[i])
                    {
                        mss="PASS";                
                    }
                    else
                    {
                        mss="FAIL";
                    }
                    if(a[i] instanceof Ongoing_Assessment)
                    {
                        Ongoing_Assessment OA=(Ongoing_Assessment) a[i];
                        mss1=OA.toString();
                    }
                    if(b[i] instanceof Final_Exam_Assessment)
                    {
                        Final_Exam_Assessment FEA=(Final_Exam_Assessment) b[i];
                        mss2=FEA.toString();
                    }
                    if(mss.equals("PASS"))
                    {
                        if((CM[i]+FM[i])>=91 &&(CM[i]+FM[i])<=100)
                        {
                            grade="A+";
                            grade2=4.00;
                        }
                        else if((CM[i]+FM[i])>=80 &&(CM[i]+FM[i])<=90)
                        {
                            grade="A";
                            grade2=4.00;

                        }
                        else if((CM[i]+FM[i])>=75 &&(CM[i]+FM[i])<=79)
                        {
                            grade="A-";
                            grade2=3.67;

                        }
                        else if((CM[i]+FM[i])>=70 &&(CM[i]+FM[i])<=74)
                        {
                            grade="B+";
                            grade2=3.33;

                        }
                        else if((CM[i]+FM[i])>=65 &&(CM[i]+FM[i])<=69)
                        {
                            grade="B";
                            grade2=3.00;
                        }
                        else if((CM[i]+FM[i])>=60 &&(CM[i]+FM[i])<=64)
                        {
                            grade="B-";
                            grade2=2.67;

                        }
                        else if((CM[i]+FM[i])>=55 &&(CM[i]+FM[i])<=59)
                        {
                            grade="C+";
                            grade2=2.33;

                        }
                        else if((CM[i]+FM[i])>=50 &&(CM[i]+FM[i])<=54)
                        {
                            grade="C";
                            grade2=2.00;

                        }
                    }
                    else if(mss.equals("FAIL"))
                    {
                        if((CM[i]+FM[i])>=47 &&(CM[i]+FM[i])<=49)
                        {
                            grade="C-";
                            grade2=1.67;
                        }
                        else if((CM[i]+FM[i])>=44 &&(CM[i]+FM[i])<=46)
                        {
                            grade="D+";
                            grade2=1.33;

                        }
                        else if((CM[i]+FM[i])>=40 &&(CM[i]+FM[i])<=43)
                        { 
                            grade="D";
                            grade2=1.00;

                        }
                        else if((CM[i]+FM[i])>=30 &&(CM[i]+FM[i])<=39)
                        {
                            grade="E";
                            grade2=0.67;

                        }
                        else if((CM[i]+FM[i])>=0 &&(CM[i]+FM[i])<=29)
                        {
                            grade="F";
                            grade2=0.00;

                        }
                    }
                    if(mss.equals("PASS"))
                    {
                        **strong text**pw1.print(mss1+mss2+"\t"+df.format((CM[i]+FM[i]))+"%\t\t"+mss+"\t"+grade+"\t"+df.format(grade2));
                        pw1.println();
                    }
                    else
                    {
                        pw2.print(mss1+mss2+"\t"+df.format((CM[i]+FM[i]))+"%"+mss+"\t"+grade+"\t"+df.format(grade2));
                        pw2.println();
                    }
                }
                br.close();
                pw1.close();
                pw2.close();
            }

1 个答案:

答案 0 :(得分:0)

文件关闭后无法重新打开。不重新创建对象是不可能的。

有关更多信息,请参见parameter pack