Results 1 to 10 of 10
  1. #1
    ryanlbowen is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default 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());
    				}
    		
    			}
    		
    		}
    		
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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?

  3. #3
    ryanlbowen is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    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 :-)

  4. #4
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    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:

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by RamyaSivakanth View Post
    Hi,
    To FileWriter constructor, set append flag to true.Then content will be appended.
    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.

  6. #6
    ryanlbowen is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    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!

  7. #7
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    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
    Ramya
    Ramya:cool:

  8. #8
    ryanlbowen is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

    Default

    OK. Sorry for all the trouble, but I have added this (I think) and it's still overwriting the text. :-/

    Java 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());
    		}
    		
    	}
    and then this is Main

    Java Code:
    				boolean append = true;
    				FileWriter(file, append);
    I'm really sorry - I haven't Java programmed in some time...

  9. #9
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    765
    Rep Power
    6

    Default

    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:

  10. #10
    ryanlbowen is offline Member
    Join Date
    Jul 2009
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Replies: 7
    Last Post: 08-17-2010, 06:00 PM
  2. Read and edit text file
    By VinTiger in forum New To Java
    Replies: 5
    Last Post: 05-14-2009, 01:18 AM
  3. save text into xml in java
    By Omarero in forum New To Java
    Replies: 8
    Last Post: 10-26-2008, 02:19 AM
  4. Replies: 5
    Last Post: 08-11-2007, 03:14 AM
  5. Replies: 1
    Last Post: 07-18-2007, 11:20 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •