CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2008
    Posts
    20

    How to write more than one String to file?

    I have a program that writes data to a file as part of its functionality, however the way it is programmed means it can only write one String and i wish for it to be able to write more than this; a sentence for example. I would like to know how i can achieve this, any help guys?

    Firstly (as the code is at the moment), in the main method, the user is asked if they would like to write data to the file (the path to which in this particular case is stored in the "path" parameter) and if the answer is equal to "yes" then the data is written by the code in the relevant section of the If statement that can be seen in the code below:
    Code:
    	    System.out.println("\nWould you like to write data to the file? (yes\\no)\n");
    	  
    	    answer1 = in.next();
    	  
    	    answer[0] = "yes";
    	    answer[1] = "no";
    	    
    	    if(answer1.equalsIgnoreCase(answer[0]))
    	    {	  
    	        System.out.println("\nEnter data to write to ADS:\n");
    	        answer2 = in.next();
    	        writeToADS.write(path, answer2);	 
    	    }	
    	    else if(answer1.equalsIgnoreCase(answer[1]))
    	    {
    	    	System.out.println("\nNo data was written to the ADS\n");
    	    }
    The code that does the writing is in a class called writeToFile and can be seen below:

    Code:
    import java.io.*; 
    
    public class writeToFile
    {
    	public static void write(String path, String answer3)
    	{
    	    try
            {
    		FileOutputStream out = new FileOutputStream(path); 
    		                                                  
    		out.write(answer3.getBytes());
    		out.close();                                        
    		    
    		System.out.println("\nData was written to file\n");
    		    
    		}
    	    catch(Exception e)
    	    {
    	    	System.out.println("Exception has been thrown :" + e); 
    	    }
    	}
    }
    So how can this be modified to write a few Strings to the file and not just one? example code would be brilliant, as i haven't much time to get this modification done.

    Thanks for any ideas and help guys

  2. #2
    Join Date
    May 2009
    Posts
    5

    Re: How to write more than one String to file?

    --use Do- while loop inside if then u can write more than one strings


    if (answer1.equalsIgnoreCase(answer[0]))
    {
    do
    {
    System.out.println("\nEnter data to write to ADS:\n");
    String answer2 = br.readLine();
    WriteToFile.write(path, answer2);

    System.out.println("\nWould you like to continue ? (yes\\no)\n");
    answer1 = br.readLine();
    } while (answer1.equalsIgnoreCase(answer[0]));

    } else if (answer1.equalsIgnoreCase(answer[1]))
    {
    System.out.println("\nNo data was written to the ADS\n");
    }

  3. #3
    Join Date
    Apr 2007
    Posts
    442

    Re: How to write more than one String to file?

    FileOutputStream has a constructor, that allows you to designate if the data is added to the files existing content, or if it replaces all. I propose you give that method of your a boolean param, and use that in the FileOutPutStreams constructor.

    However, there is not necessarily lot of sense in writing stuff piecemeal to a File. So what your writeToFile class could provide, is a dummy context, in which these bits and pieces are stored, when all expected data is in, write all that to the actual file. Simplest way this can be done simply providing a StringWriter instance. You can append as many String to it as you like, and the call your file writing method using the StringWriters toString() as data.
    Last edited by Londbrok; June 11th, 2009 at 02:13 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
  •  





Click Here to Expand Forum to Full Width

Featured