java io - File output deletes all the content -
i trying make program removes text file. text remove , file path provided command line arguments. goes fine, when open file after program has finished running, it's empty. doing wrong?
import java.io.file; import java.io.ioexception; import java.io.printwriter; import java.util.scanner; public class remove { public static void main(string[] args) throws ioexception{ if(args.length != 2) { system.out.println("usage : java remove stringtoremove filepath"); system.exit(1); } string stringtoreplace = args[0]; string path = args[1]; file file = new file(path); if(!file.exists()) { system.out.println("no such file exists!"); system.exit(2); } scanner input = new scanner(file); printwriter output = new printwriter(file); while(input.hasnext()) { string currentline = input.nextline(); currentline = currentline.replaceall(stringtoreplace, ""); output.println(currentline); } input.close(); output.close(); system.out.println("operation successful"); } }
opening file writing clears file, if input , output files same, input cleared , nothing happen because there no lines.
Comments
Post a Comment