473,383 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Why the file.txt is not updated inside the IDE (using BufferedWriter)?

Hi all ,I came across the internet for such a method to write to a file,although I am taking java but never really learned about buffered writer instead I took Printstream class;my question is why the records of the file created using this method are not updated (if Ineed to add records) inside IntellijIdea,instead you have always to check the file using Notepad,thanks for any reply here is the code:

Expand|Select|Wrap|Line Numbers
  1. public void writeToFile(String filename){
  2.  
  3.  
  4.             BufferedWriter bufferedWriter = null;
  5.  
  6.             try {
  7.  
  8.                 // Construct the BufferedWriter object
  9.                 bufferedWriter = new BufferedWriter(new FileWriter(filename));
  10.  
  11.                 // Start writing to the output stream
  12.                 bufferedWriter.write("Writing line one to file");
  13.                 bufferedWriter.newLine();
  14.                 bufferedWriter.write("Writing line two to file");
  15.                 bufferedWriter.newLine();                        
  16.                 bufferedWriter.write("Writing line three to file");
  17.             } catch (FileNotFoundException ex) {
  18.                 ex.printStackTrace();
  19.             } catch (IOException ex) {
  20.                 ex.printStackTrace();
  21.             } finally {
  22.  
  23.                 // Close the BufferedWriter
  24.                 try {
  25.                     if (bufferedWriter != null){
  26.                         bufferedWriter.flush();
  27.                         bufferedWriter.close();
  28.                     }
  29.                 } catch (IOException ex) {
  30.                     ex.printStackTrace();
  31.                 }
  32.             }
  33.         }
Apr 8 '09 #1
2 2271
dmjpro
2,476 2GB
Please use "code tag". By the way i tested this code, it's running successfully. But what do you mean by "inside the IDE"?
Apr 9 '09 #2
JosAH
11,448 Expert 8TB
Each time you run that code it'll create a new file (the old contents are lost). If that's not what you want read the API documentation for the FileWriter class; it also has a constructor that appends your data to the already existing contents.

kind regards,

Jos
Apr 9 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Konrad Den Ende | last post by:
I tried to write some Unicode-16 characters (that were displayed correctly, as expected, on the screen) to a file but it didn't work out very well. I have those in an char as well as a String. Both...
2
by: slukacs | last post by:
hello one and all, i am still new to java but becoming ever so more efficient. i need some advice and guidance on big strings. i am debugging a special counter that i created in java and i am...
1
by: Row | last post by:
Hi, I am having trouble writting from a JTextArea to a flat text file (locally) I have tried using string tokenizer to tokenize the string first but with no luck, Im stuck on the one :( im...
6
by: gauravkhanna | last post by:
import java.io.*; class Automation1 implements Runnable { int iterationCount=0; Thread t; FileReader fr; //FileWriter fw; BufferedReader br; ...
9
by: asenthil | last post by:
Hai to all,, i had to tried to retrive and write a single row to a file from the database.. But dont know to write all the retrived rows to a file from the database.. how to do that... ...
1
by: PvtBillPilgrim | last post by:
I need to write a method called save that takes a String as a parameter, and returns a boolean value. The parameter represents a filename, and the method should save the contents of one of my classes...
6
by: topala1984 | last post by:
hello all excuse my english, I`m a beginer in java and I want to remove duplicate cities + rearrenging file`s lines elements from one 150 mb file, in my file I have that: ...
2
by: minouparhizkar | last post by:
hi could anyone helping me to finish this i have no idea how to implement the program in java im trying to grabbing the pixel from image and then convert it to the txt file .i did that but it didnt...
2
by: redcodeheaven | last post by:
Hello all,I have 2 methods inside the same class ,for some reason I couldn't output the values to the file,I am always getting result null(because the data fields are Strings) as a value when I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.