Results 1 to 6 of 6
Thread: Read and edit text file
- 05-12-2009, 08:44 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
- 05-12-2009, 09:58 AM #2
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
First read the entire file (if it's not too big, that is) using
Put every line in a slot of an array.Java Code:BufferedReader file = new BufferedReader(new FileReader(FILE)); file.readLine(); file.close();
Edit the things you want to edit
When done, write everything to a completely new file
Something like that ;)Java Code:BufferedWriter file = new BufferedWriter(new FileWriter(FILE)); file.write(String); file.flush(); file.close();
Of course, this is just one of the many possibilities.
~MattI die a little on the inside...
Every time I get shot.
- 05-12-2009, 10:09 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
I believe BufferedReader waits until there is another line to read, so unless you do some checking, it will freeze. Another way to read the file is to use
As for writing the file, I can't help you there, srry.Java Code:Scanner scan = new Scanner(FILE); ArrayList<String> lines = new ArrayList<String>(); while(scan.hasNext()){ lines.add(scan.readline()) } //and closing the scanner and conversion of the ArrayList to an array
Hope this helps,
Singing BoyoIf the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 05-12-2009, 10:15 AM #4
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Good point, forgot about that ;)
That should fix it ;)Java Code:String line; while ((line = file.readLine()) != null) // Put 'line' in arrayI die a little on the inside...
Every time I get shot.
- 05-13-2009, 05:01 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 32
- Rep Power
- 0
thanks guys, I'll try this:)
- 05-14-2009, 01:18 AM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
still won't work... cant call file.readLine() if there is no line to be read :D
Use a Scanner :pLast edited by Singing Boyo; 05-15-2009 at 02:10 AM.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Read from a text file?
By aaronfsimons in forum New To JavaReplies: 3Last Post: 05-01-2009, 04:42 AM -
read from text file
By rayda in forum New To JavaReplies: 5Last Post: 04-10-2009, 03:51 AM -
Read and modify text file
By heartysnowy in forum New To JavaReplies: 27Last Post: 11-19-2008, 10:03 AM -
[SOLVED] How do I read from a text file
By matzahboy in forum New To JavaReplies: 5Last Post: 11-17-2008, 04:47 AM -
How to read a text file from a Java Archive File
By Java Tip in forum Java TipReplies: 0Last Post: 02-08-2008, 09:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks