Read and edit text file
I'm trying to make a program that not only reads a text file into an array, but i also need to add the ability to edit the text file, as well as navigate back and forth through the entire file. I'm more than a little bit stuck so any help is greately appreciated.
First read the entire file (if it's not too big, that is) using
Put every line in a slot of an array.
BufferedReader file = new BufferedReader(new FileReader(FILE));
Edit the things you want to edit
When done, write everything to a completely new file
Something like that ;)
BufferedWriter file = new BufferedWriter(new FileWriter(FILE));
Of course, this is just one of the many possibilities.
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.
Scanner scan = new Scanner(FILE);
ArrayList<String> lines = new ArrayList<String>();
//and closing the scanner and conversion of the ArrayList to an array
Hope this helps,
Good point, forgot about that ;)
That should fix it ;)
while ((line = file.readLine()) != null)
// Put 'line' in array
thanks guys, I'll try this:)
still won't work... cant call file.readLine() if there is no line to be read :D
Use a Scanner :p