0

every time I write anything in an empty file and run the main method the whole empty file gets erased

package assignment1;

import java.util.*;
import java.io.*;

public class TestFoodProduct {

 
    public static void main(String[] args) throws FileNotFoundException  {
        Scanner inFile = new Scanner (new FileReader ("food.data"));
        PrintWriter pw = new PrintWriter ("food.data");
    }
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 4
    I don't see any calls to `pw.print` or anything like that. Are you sure you're even printing your data? If so, can we please see the whole code? – dimitar.bogdanov Sep 13 '21 at 13:01
  • 1
    Your file is getting overwritten because you are reading and writing to the same file (though you don't actually read or write anything in your code yet). – Nexevis Sep 13 '21 at 13:13
  • @Nex That's not necessarily why the file is overwritten. It's only because `new PrintWriter(String)` alone clears the file. The scanner is irrelevant, but will certainly cause other issues – OneCricketeer Sep 13 '21 at 13:16

0 Answers0