-2
String testLine=id+" "+name+" "+NewQuizz;

                Scanner ReadScanner3 = new Scanner(new FileInputStream("input.txt"));
                FileOutputStream Out=new FileOutputStream("temp.txt");
                PrintWriter pwrite=new PrintWriter(Out);
                while(ReadScanner3.hasNextLine()) {
                      line=ReadScanner3.nextLine();
                    id=Integer.parseInt(line.substring(0, line.indexOf(' '))) ;



                    pwrite.println(infoAfterModfiy);


                }

                System.out.print("after grade modifcation:");
                String printModfiy=SearchID+"\t\t"+name+"\t\t"+printNewQuizz;
                System.out.println(printModfiy);
                pwrite.close();
                Scanner ReadScanner4 = new Scanner(new FileInputStream("temp.txt"));
                FileOutputStream Out2=new FileOutputStream("input.txt");
                PrintWriter pwrite2=new PrintWriter(Out,true);
                while(ReadScanner4.hasNextLine()) {
                    String Line=ReadScanner4.nextLine();
                    pwrite2.println(Line);
                }
                pwrite2.close();

            }

hello hope you all good. I have problem with the overwriting if i changed the quize grade and I display it again it delete everything in input.txt

sample input

91007 Ahmad Said 50.0 
91004 Hassan Khan 45.5 
91003 Suleiman Wasim 72.6 
91002 Majed Sameer 60.0 
91006 Muhammad Adel 85.5 
91005 Muhsim Zuheir 70.0 
91001 Muneeb Abdullatif 30.0
  • 2
    It is good practice to include neat, readable code that would run if someone were to copy/paste it into an IDE - [reprex] – sleepToken Dec 11 '19 at 19:36
  • Does this answer your question? [How to write data with FileOutputStream without losing old data?](https://stackoverflow.com/questions/8544771/how-to-write-data-with-fileoutputstream-without-losing-old-data) – mypetlion Dec 11 '19 at 19:42
  • @mypetlion no not really – Abdullah Aim Dec 11 '19 at 20:00
  • 1
    Nevermind, I think I see what the problem is. You're outputting to your input file. Look at this line: `FileOutputStream Out2=new FileOutputStream("input.txt");` – mypetlion Dec 11 '19 at 20:04

1 Answers1

0

I came here a bit late; however, here is what you can do to make your code a little cleaner. The FileOutputStream can be removed along with the additional Scanner and PrintWriter objects to perform what you are trying to achieve.

consider:

// Just an example 
double[] newGrade = {30.0, 37.0, 55.0, 75.0, 80.0, 90.0, 95.0};
int i = 0;
String outputTxt = "", oldGrade = "";
Scanner scan = new Scanner(new File("input.txt"));
PrintWriter writer =new PrintWriter("output.txt");
while(scan.hasNext()) {
    String tmp = "";
    tmp += scan.next();
    tmp += " " + scan.next();
    tmp += " " + scan.next();
    oldGrade = scan.next();
    tmp += " " + oldGrade;

    if (i <= newGrade.length) {
    // Update grade here
    outputTxt += tmp.replace(oldGrade, newGrade[i] + "\r");
    }
    i++;
}
writer.print(outputTxt);
scan.close();
writer.close();

input.txt

91007 Ahmad Said 50.0 
91004 Hassan Khan 45.5 
91003 Suleiman Wasim 72.6 
91002 Majed Sameer 60.0 
91006 Muhammad Adel 85.5 
91005 Muhsim Zuheir 70.0 
91001 Muneeb Abdullatif 30.0

output.txt - grades updated.

91007 Ahmad Said 30.0
91004 Hassan Khan 37.0
91003 Suleiman Wasim 55.0
91002 Majed Sameer 75.0
91006 Muhammad Adel 80.0
91005 Muhsim Zuheir 90.0
91001 Muneeb Abdullatif 95.0