Dear moderators and members,
A greeting of peace.
Good day to everyone, I hope everyone is in good health upon reading this thread of mine. Well, I have this program created just a while ago:
import java.io.*; public class FileWriteAndRead{ public static void main(String args[]) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bfw = new BufferedWriter(new FileWriter("sample.txt")); BufferedReader bfr = new BufferedReader(new FileReader("sample.txt")); String number=""; System.out.print("Please enter a number: "); try{ number = br.readLine(); bfw.write(number); }catch(Exception ioe){ } System.out.println("The name was successfully written in a file."); try{ name = bfr.readLine(); name = ((Integer.parseInt(name)) *100) + " "; bfw.write(name); }catch(FileNotFoundException fnfe){} System.out.println("The new content of the file is: "+name); bfw.close(); bfr.close(); }//end of main }//end of class
My plan is to enter a number, write it in a text file, get it again so that it will be multiplied by 100, then the result will be written again on the file, thus I want to overwrite my file, but the sad thing part is, the result is just keep on adding on the file (Example, if I enter 2, the text file has a content of 2200 instead of 200 alone (the number 2 was not deleted/overwritten). What am I doing wrong in my program?Am I missing something? I hope everyone with a good heart will correct me in my program. By the way, I am using JDK6 update 12 (I guess it has nothing to do with it, right?). Thank you in advance and more power to everyone. God bless everyone.
Respectfully yours,
MarkSquall