Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: write text to a file help

  1. #1
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Question write text to a file help

    i dont have much knowledge on streams output streams mostly , and right now i need to make a method that will write text to a file

    here's all that i have set up >.<
    info[0] = the file name
    info[1] = the stuff to write to that file

    public void fileWriter(String[] info){
     
    	}

    yes , i know its bare XP but thats about all i know about the output streams
    Programming: the art that fights back


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: write text to a file help

    If you look at the bottom of this page there is a list of similar threads.

    Have a look at http://www.javaprogrammingforums.com...sing-java.html

    // Json

  3. The Following User Says Thank You to Json For This Useful Post:

    wolfgar (October 20th, 2009)

  4. #3
    Member wolfgar's Avatar
    Join Date
    Oct 2009
    Location
    the middle of the woods
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: write text to a file help

    oh wow , i never noticed that b4 XP *fails*
    thanks for the help though ^_^
    Programming: the art that fights back

  5. #4
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: write text to a file help

    Hello to all,
    I have managed to write to a file but is keeps writing on the same line.
    I’ve tried «\n" but the .txt file recognizes it as a character.
    any ideas?
    thank you

  6. #5
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: write text to a file help

    Print the following to the file stream.
    System.getProperty("line.separator");

    Chris

  7. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: write text to a file help

    public static boolean saveStringToFile(String fileName, String saveString)
    	{
    		boolean saved = false;
    		BufferedWriter bw = null;
     
    		try 
    		{
    			// bw   = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
    			bw= new BufferedWriter(new FileWriter(fileName));
    			try
    			{
    					//bw.println();
    					bw.write(saveString);
    					System.getProperty("line.separator");
    					//bw.newLine();
    					saved = true;	
    			}//end of try
    			finally
    			{
    				bw.close();
    			}//end of finally			
    		}//end of try
    		catch (IOException ex)
    		{
    			ex.printStackTrace();
    		}//end of catch
    		return saved;
    	}//end of saveStringToFile method

    this is my code.
    i tried the line.separator as well as .newlive() and both didn't work

  8. #7
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: write text to a file help

    If using the BufferedWriter class then the .newLine() function would work. However, i'm not to sure where you are specifying that there should be a new line printed. From my understanding you are passing a String of text (one line) to a function which then write that line to the file and then prints a new line.


    Could you please give an example of inputs to the function and the desired output.


    Regards,
    Chris

  9. #8
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: write text to a file help

    Well am using a queue simulation and I want to write every line of the queue
    The code below shows more details
    if(tillQueue.peek() == null)
       System.out.println(i+"\t"+"\t"+ getCurrentQLength()+"\t"+"Queue Empty"+"\t"+"Info Saved: "+saveStringToFile("Queue_Info.txt", tillQueue.toString()));
     
    else
      System.out.println(i+"\t"+ "\t"+ getCurrentQLength()+ "\t"+tillQueue.toString()+"\t"+"Info Saved: "+saveStringToFile("Queue_Info.txt", tillQueue.toString()));

  10. #9
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: write text to a file help

    Try opening the file in append mode, new FileWriter(fileName, true); Also you will need to loop trhough each line of the Queue rather than just dumping it onto the saveStringToFile() function. But yes open in amend, print a line, print a blank line (bw.newLine()), close repeat.

    Regards,
    Chris

Similar Threads

  1. Writing in a file using Java
    By JavaPF in forum File Input/Output Tutorials
    Replies: 4
    Last Post: December 17th, 2011, 04:33 PM
  2. Creating variables from a text file
    By Larz0rz in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 4th, 2009, 11:57 PM
  3. printing output to console & to a text file at the same time...
    By prasanna in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 26th, 2009, 03:43 AM
  4. Java program to reduce spaces between the words in a text file
    By tyolu in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 13th, 2009, 07:17 AM
  5. [SOLVED] Enhancement in program of removing whitespace from text file
    By John in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: April 27th, 2009, 09:36 AM

Tags for this Thread