Results 1 to 10 of 10
Thread: Edit/Save Text File - Problems
- 07-20-2009, 02:36 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 6
- Rep Power
- 0
Edit/Save Text File - Problems
Hello all,
I am writing a little program that should simply add a title and description to an existing text file. However, my problem is that every time I enter a title and description, it overwrites everything in the program.
Here is my code -any clues as to what's wrong?
Java Code:public class reference { /** * @param args */ public static int countLines (File file) throws IOException { int lineCount = 0; Reader reader = new InputStreamReader(new FileInputStream(file)); char[] buffer = new char[5000]; for ( int charsRead = reader.read(buffer); charsRead >= 0; charsRead = reader.read(buffer) ) { for (int charIndex = 0; charIndex < charsRead; charIndex++ ) { if ( buffer[charIndex] == '\n' ) lineCount++; } } reader.close(); return lineCount; } public static void main(String[] args) { // TODO Auto-generated method stub String title = ""; String test = ""; String descrip = ""; for ( int count=0; count < 1137; count++ ) { test = JOptionPane.showInputDialog(null, "Entry Title: "); if ( !test.equalsIgnoreCase("abort") ) { title = test; } else { count = 1138; try { JOptionPane.showMessageDialog(null, countLines(new File("reference.txt"))); } catch (Exception e) { System.out.println("Problem, exception = " + e); e.printStackTrace(System.out); } System.exit(0); } descrip = JOptionPane.showInputDialog(null, "Entry Description: "); JOptionPane.showMessageDialog(null, title + ": \n" + descrip); try{ FileWriter entry = new FileWriter("reference.txt"); BufferedWriter out = new BufferedWriter(entry); out.write(title + ": " + descrip + "\n"); out.close(); }catch (Exception e){ System.err.println("Error: " + e.getMessage()); } } } }
-
Do understand that when you are writing to a file, you overwrite that file, that especially when writing to the beginning of a file, that there's no way for the operating system or the disk hardware to magically move the old bits over?
So with this in mind, after your BufferedWriter writes the title and description, where do you write out the remaining contents of the file? The contents that you will need to read in first?
- 07-20-2009, 03:41 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 6
- Rep Power
- 0
Ok - so you're saying that I need to copy everything, add the new title and description, and then dump it all into a new file?
I'd love to just add the string to the end of the text file...
Sorry...I'm not that great at this - but thanks for the help :-)
- 07-20-2009, 03:51 PM #4
Hi,
To FileWriter constructor, set append flag to true.Then content will be appended.
Gothru the api
FileWriter(String fileName, boolean append)
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.Ramya:cool:
-
As I understand the original problem, he is adding text to the beginning of the file. If so, this suggestion above won't work (and is why I didn't suggest it initially). He'll have to stream through the file writing to a different file as he reads, then re-name the newly written file to the old file when he's done.
- 07-20-2009, 04:52 PM #6
Member
- Join Date
- Jul 2009
- Posts
- 6
- Rep Power
- 0
Thanks for your help guys. Actually, appending to the end is fine. Sorry I didn't make that clearer.
I'll try the FileWriter constuctor - thanks!
- 07-20-2009, 05:01 PM #7
Hi Ryanlbowen,
Be specific with ur requirement from next time.
Hi Fubarable,
I'm able to get what u are trying to say.U guys are making me to learn a lot and helping me to give solutions.
-Regards
RamyaRamya:cool:
- 07-20-2009, 05:05 PM #8
Member
- Join Date
- Jul 2009
- Posts
- 6
- Rep Power
- 0
OK. Sorry for all the trouble, but I have added this (I think) and it's still overwriting the text. :-/
and then this is MainJava Code:public static void FileWriter (String file, boolean append) { try { FileWriter entry = new FileWriter("reference.txt"); BufferedWriter out = new BufferedWriter(entry); out.write( "\n" + file + "\n" ); out.close(); }catch (Exception e){ System.err.println("Error: " + e.getMessage()); } }
I'm really sorry - I haven't Java programmed in some time...Java Code:boolean append = true; FileWriter(file, append);
- 07-20-2009, 05:10 PM #9
Hi,
Gothru the corrected piece of code.It should work.
Java Code:try { FileWriter entry = new FileWriter("reference.txt",true); BufferedWriter out = new BufferedWriter(entry); out.append(title + ": " + descrip + "\n"); out.close(); }catch (Exception e) { System.err.println("Error: " + e.getMessage()); }Last edited by RamyaSivakanth; 07-20-2009 at 05:15 PM.
Ramya:cool:
- 07-20-2009, 05:23 PM #10
Member
- Join Date
- Jul 2009
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
How can I save the content of textfield in a text file?
By fred in forum Java AppletsReplies: 7Last Post: 08-17-2010, 06:00 PM -
Read and edit text file
By VinTiger in forum New To JavaReplies: 5Last Post: 05-14-2009, 01:18 AM -
save text into xml in java
By Omarero in forum New To JavaReplies: 8Last Post: 10-26-2008, 02:19 AM -
Edit JPanel Text During Runtime...from another class
By bdn1404 in forum New To JavaReplies: 5Last Post: 08-11-2007, 03:14 AM -
problems trying to view the contents of a text file in JTextArea
By warship in forum New To JavaReplies: 1Last Post: 07-18-2007, 11:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks