Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

java output multiple lines to file

  • 21-11-2003 12:11am
    #1
    Registered Users, Registered Users 2 Posts: 1,362 ✭✭✭


    hi , im tryin to output a lot of text that i want hardcoded in my program to a file, eg


    FileOutputStream out; // declare a file output object
    PrintStream p; // declare a print stream object

    try
    {
    // Create a new file output stream
    // connected to "myfile.txt"
    out = new FileOutputStream("myfile.txt");

    // Connect print stream to the output stream
    p = new PrintStream( out );


    ///////////////////////////here is problem...////////////////////////////////////
    p.println ("text text text text
    text text text text
    text text text text
    text text text text
    text text text text
    ");

    p.close();
    }
    catch (Exception e)
    {
    System.err.println ("Error writing to file");
    }



    but obviously i cant do that, does anyone know the syntax for allowing multiple lines of text to be outputted to a file?


Comments

  • Closed Accounts Posts: 302 ✭✭Auburn


    You could write the text to a StringBuffer. That would work I think

    e.g.

    x = new StringBuffer()
    x.append("text text text text
    text text text text
    text text text text
    text text text text
    text text text text
    ");

    Can't remember how you output that (my Java's a bit rusty :D ) but you should find stuff in the API or on Java.sun.com forums


  • Registered Users, Registered Users 2 Posts: 1,362 ✭✭✭tw0nk


    eeep , nope that didnt work , it still looks for the quotation marks at the end of the line , i have looked on java.sun.com for ages and in the API's and i cant seem to find a solution, i know it must be fairly simple but i just cant find the syntax, thanks for your help anyways auburn


  • Registered Users, Registered Users 2 Posts: 4,196 ✭✭✭deadl0ck


    I don't get it ?

    What's the problem ?

    Can't you just put it all on 1 line and put a \nbetween them for the newlines :

    p.println ("text text text text\ntext text text text\ntext text text text\ntext text text text\ntext text text text");

    Are you getting a compile error or what ?


  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    As dealock says, the compiler ignores when you go onto a new line in your program.

    In theory, you could write your entire program on one line, or one way too many lines, with each word on a new line.

    Stick in the \n character and you'll be fine.


  • Closed Accounts Posts: 1,525 ✭✭✭vorbis


    don't open that text file in notepad afterwards, it doesn't recognise \n. use something like wordpad to check if it worked.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 68,317 ✭✭✭✭seamus


    Originally posted by vorbis
    don't open that text file in notepad afterwards, it doesn't recognise \n. use something like wordpad to check if it worked.

    Notepad will see it just fine.

    It doesn't recognize a literal LF-CR character, but as far as it's concerned, the "\n" in the source code is a backslash followed by an n, ie two separate characters.

    :)


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    In property files there is a control character to span new lines think it is ...

    \

    at the end of the line or maybe

    /

    (been a while and I tend not to do it as it confuses the hell out of people).

    could put the text into a property file and use that? otherwise the \n will do.


  • Registered Users, Registered Users 2 Posts: 597 ✭✭✭bambam


    p.println ("text text text text\n" +
    "text text text text\n" +
    "text text text text\n" +
    "text text text text\n" +
    "text text text text");


  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    that should do it. However,as Auburn suggested, better practice would be to buffer (Quick example from some pre-existing code= )

    <code>

    private BufferedWriter outStream;
    private String fileName="file.txt";

    /**
    * Writes to file
    * @param String this is the string that is written to file
    */

    public boolean writeLogEvent( String s ){
    try{
    BufferedWriter outStream = new BufferedWriter( new FileWriter( fileName, true ) );
    String writeFormattedString;

    writeFormattedString= s;

    outStream.write(writeFormattedString + "\n");
    // testing:
    // System.out.println(writeFormattedString);

    //close stream:
    outStream.flush();
    outStream.close();

    return true;
    }
    catch(IOException e)
    {
    }

    return true;
    }

    </code>


  • Registered Users, Registered Users 2 Posts: 19,396 ✭✭✭✭Karoma


    Also, nice resource:

    java almanac (Writing to files)


  • Advertisement
Advertisement