Results 1 to 8 of 8
  1. #1
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Post Creating a new line

    I am learning how to save to a text file, and i am having some trouble creating a new line. I'm sure it is a quick and easy fix, but I can't find my problem. Here is my code.

    Java Code:
    package testsave;
    import java.io.*;
    
    public class TestSave {
    
        public static void main(String[] args) {
            String text = "Hello World!";
            String text1 = "Hello to you too!";
            try{
                FileWriter file = new FileWriter(text +".txt");
                file.write(text + " \n");
                file.write(text1 +" \n");
            }catch(IOException ex){
                ex.printStackTrace();
            }
        }
    }
    When i run this, i get :

    Hello World! Hello to you too!

    Thanks for the help!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    i get...
    Do you mean that this is what you see when you view the file in some viewer software? I ask because one OS inflicts on its users a text editor (Notepad) that cannot render newlines.

    There are two ways to go:

    * Use a BufferedWriter which has a newLine() method which will write whatever your Java runtime considers to be correct for your OS. (In which case you will have to deal with the different ways your file might be interpreted on different systems)

    * Live with writing the newline as '\n'. (In which case your file is portable, but you will have to use a reliable text editor/viewer.)

    [Edit]

    Also, close file after you are finished using it. As written you may not see any output at all in the file.
    Last edited by pbrockway2; 08-06-2011 at 07:44 AM.

  3. #3
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    when i say "i get" i mean to say, when i open the my .txt , it is what i see. I am using regular notepad for viewing the file i wrote. Thanks for the quick reply!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    OK. Post back if you get stuck using BufferedWriter. PrintWriter is another way to go: System.out is an instance of PrintWriter, so you are already accustomed to how it works. Create one that writes to a file then println() will be good.

    textpad is a good plain text editor for Windows.

  5. #5
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    Thanks! BufferedWriter worked great. I used the newLine(); method to make a new line, but what was the println() thing you were talking about. I don't understand that.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    but what was the println() thing you were talking about. I don't understand that.
    I was thinking along these lines:

    Java Code:
    package testsave;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class TestSave {
    
        public static void main(String[] args) {
            String text = "Hello World!";
            String text1 = "Hello to you too! (using a PrintWriter)";
            PrintWriter file = null;
            try {
                file = new PrintWriter(text +".txt");
                [color=blue]file.println(text);
                file.println(text1);[/color]
            } catch(IOException ex){
                ex.printStackTrace();
            } finally {
                if(file != null) {
                    file.close();
                }
            }
        }
    }
    The effect is much the same as you would get by saying

    Java Code:
    System.out.println(text);
    System.out.println(text1);
    Closing the file in this way is covered in Oracle's Tutorial in the Exceptions lesson.

    [Edit] I misspoke before: System.out is actually an instance of PrintStream, not PrintWriter. It doesn't make a lot of difference in this case as both have similar methods.
    Last edited by pbrockway2; 08-06-2011 at 08:34 AM.

  7. #7
    Huskies's Avatar
    Huskies is offline Member
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    Oh ok, i get it. Thanks for all the help!

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. Constructors pls explan the code line by line in comments
    By vibaviattigala in forum New To Java
    Replies: 1
    Last Post: 02-19-2011, 05:03 AM
  2. Replies: 1
    Last Post: 02-13-2011, 04:09 PM
  3. Replies: 2
    Last Post: 07-02-2010, 03:20 PM
  4. Replies: 4
    Last Post: 09-10-2009, 11:18 AM
  5. Creating a boundary line in java
    By Riz in forum New To Java
    Replies: 2
    Last Post: 09-03-2009, 01:54 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
  •