Results 1 to 5 of 5
  1. #1
    Yogesh_P is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default Problem writing multiple strings to a text file

    Hi

    I am writing a program where I will have to write many Strings into a text file, so I want to create a separate method to which I can pass a String and it will write the String to a new line in the file. But how many Strings I may pass it only writes the last one. Can someone please help me debug.

    Here is the code:

    Java Code:
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    
    
    public class Example 
    {
    	String logFile;
    	BufferedWriter fileWrite;
    	public static void main(String[] args)
    	{
    		Example ex = new Example();
    		ex.logFile = "log0.txt";
    		ex.log("1st line");
    		ex.log("2nd line");
    		ex.log("3rd line");
    	}
    	
    	public void log(String str)
    	{
    		try
    		{
    			fileWrite = new BufferedWriter(new FileWriter(logFile));
    			fileWrite.write(str);
    			fileWrite.close();
    		}
    		catch(IOException ex)
    		{
    			System.out.println("Error : cannot write into log file");
    			ex.printStackTrace();
    		}
    	}
    }
    Here I want to write 3 strings to a particular file , I dont want to copy code for all file readers and writers everytime I have to write.

    Thanks and Regards

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Each time you try to write you create a new OutputStream to the file. Thus you will overwrite anything that is already in the file. Try creating the stream in "append" mode. Check the Java API.

  3. #3
    Yogesh_P is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Thank you, it works. I had not seen any example with append mode in any of the books.

    Regards

  4. #4
    Yogesh_P is offline Member
    Join Date
    Mar 2011
    Posts
    10
    Rep Power
    0

    Default

    Another query:

    How can I modify the program so that log file is created by this name whenever the program runs and then strings are appended to it.

    When I run program I dont want my results to be appended to old logfile. Any Help !

    Regards

  5. #5
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Quote Originally Posted by Yogesh_P View Post
    Another query:

    How can I modify the program so that log file is created by this name whenever the program runs and then strings are appended to it.

    When I run program I dont want my results to be appended to old logfile. Any Help !

    Regards
    Java Code:
    File f = new File("log.txt");
    if (!f.exists())
    f.createNewFile();

Similar Threads

  1. Writing multiple lines to text file at once
    By Plex in forum New To Java
    Replies: 14
    Last Post: 11-08-2010, 09:17 PM
  2. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  3. writing to text file problem
    By blumdiggity in forum Networking
    Replies: 1
    Last Post: 02-26-2010, 02:43 PM
  4. Replies: 5
    Last Post: 07-18-2008, 04:30 PM
  5. writing text to file
    By notwist in forum New To Java
    Replies: 3
    Last Post: 04-25-2008, 04:20 AM

Posting Permissions

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