First you need to read the file, and then close the file. Then start writing content to it.
By default, it opens the file in recording mode. All data is lost before you read anything.
class Virus{
public static void main(String[] args) throws Exception{
File f = new File("/Users/abafna/coding/src/abc.txt");
FileReader fr = new FileReader(f);
int count = 0;
while(fr.read()!=-1){
count++;
}
fr.close();
System.out.println(count);
FileWriter fw = new FileWriter(f);
while(count-->0){
fw.write('*');
}
fw.flush();
fw.close();
}
}
source
share