Results 1 to 10 of 10
- 01-01-2010, 01:11 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 59
- Rep Power
- 0
reading from input file and then write on it
hello everyone
I need to read from an input file and then write to the same file??
can I do this?
my code is the following
but when I write the above code and then start reading the lines from the file I have got blank fileJava Code:Scanner inFile2 = new Scanner(new FileInputStream(file2)); BufferedReader reader2 = new BufferedReader(new FileReader(file2)); //BufferedWriter writer2 = new BufferedWriter(new FileWriter(file2)); line2=null; line2 = reader2.readLine() ;
can you please help me
what is the problem of defining the reading and writing file
thanks alot
- 01-01-2010, 01:15 PM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
new FileWriter(filename,true);
FileWriter (Java 2 Platform SE v1.4.2)I die a little on the inside...
Every time I get shot.
- 01-01-2010, 02:56 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 59
- Rep Power
- 0
thanks alot
but using this method I can read lines and then write ( or adding )some lines on the file
I want to read and then clear the file and then write new lines how can I do this??
- 01-01-2010, 03:38 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
- 01-01-2010, 03:43 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 59
- Rep Power
- 0
sorry but I didn't get what you saying?? I need to read the contents of file and then clear it and then write new contents on this file
I have used the following code:
can you tell me what is the wrong in this code ?? because using the above code I can only read contents and then write on this file without clearing the previous contentJava Code:Scanner inFile2 = new Scanner(new FileInputStream(file2)); BufferedReader reader2 = new BufferedReader(new FileReader(file2)); line2=null; line2 = reader2.readLine() ; FileWriter writer2= new FileWriter(file2,true);
thanks alot
-
Read the file first, close it, and only then open it for writing.
- 01-01-2010, 04:14 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 59
- Rep Power
- 0
but if I read and then close the file and then open it for writing it will also add the new lines to the previous content it will not clear the pervious content may be I did some thing wrong with code
My code is the following:
can you please tell me what is the wrong??Java Code:Scanner inFile2 = new Scanner(new FileInputStream(file2)); BufferedReader reader2 = new BufferedReader(new FileReader(file2)); line2=null; line2 = reader2.readLine() ; // doing reading the file inFile2.close(); FileWriter writer2= new FileWriter(file2,true); writer2.write("sum is " ); writer2.close();
thanks alot
- 01-01-2010, 04:22 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,413
- Blog Entries
- 7
- Rep Power
- 17
Read my previous reply again and change your last line to this:
Reading the API documentation for that FileReader class might help too.Java Code:FileWriter writer2= new FileWriter(file2);
kind regards,
Jos
- 01-01-2010, 04:30 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 59
- Rep Power
- 0
ok thanks the problem was the true
thanks alot for your reply:)
my problem is solved
- 01-19-2010, 11:41 AM #10
Member
- Join Date
- Jan 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Error Message when reading an input file.
By Deluyxe in forum New To JavaReplies: 8Last Post: 04-26-2009, 04:02 PM -
[SOLVED] Reading an input string?!
By sfe23 in forum New To JavaReplies: 6Last Post: 02-23-2009, 04:38 AM -
Reading data from csv file based on specific input
By jaiminparikh in forum Advanced JavaReplies: 14Last Post: 02-13-2009, 09:07 PM -
Problem in reading HTML input field while uploading file
By sudipanand in forum Java ServletReplies: 1Last Post: 11-27-2008, 09:26 AM -
Reading input file into an array
By littlefire in forum New To JavaReplies: 6Last Post: 10-18-2008, 11:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks