Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Help! - How to insert a new line to a text file

  1. #1
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default Help! - How to insert a new line to a text file

    Hi all,

    I have written a seimple program to test out a bigger idea.
    This reads 2 columns from a database (about 477 rows at the moment)
    and it then writes these to a text file.
    I need to put each row on a new line in the text file but '\n' seems to just return a special character in the .txt

    Can anybody suggest how I do this?
    here is my code:
    Java Code:
    import java.sql.*;
    import java.io.*;
    import java.io.FileWriter;
    
    
    class SimpleOraJava {
     
        public static void main(String args[]) throws SQLException, IOException {
        
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        
            String serverName = "servername";
            int port = 1521;
            String user = "user";
            String password = "password";
            String SID = "nikuuat";
            String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
        
            Connection conn = DriverManager.getConnection(URL, user, password);
            String SQL = "SELECT DSTI_PROJ_REF, id FROM NIKU.ODF_CA_PROJECT where dsti_proj_act = 'ADM'";
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(SQL);
            FileWriter fout = new FileWriter("c:\\test.txt"); //Create new file stream
        
            while (rs.next()) { //cycle through result set
                  fout.write(rs.getString(1) + "\t"  + rs.getInt(2) + "\n");
            }
        
            fout.close();  //close file stream
        
            System.out.println("Complete");
            stat.close();
            conn.close();
    
      }
    
    }
    Thanks in advance,

    Matt
    Last edited by matpj; 01-12-2009 at 06:14 PM. Reason: forgot to say thank you!

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Use a BufferedWriter
    Java Code:
    BufferedWriter bw = new BufferedWriter(new FileWriter("c:\\test.txt"));
    bw.newLine();
    :)

    ~Matt
    I die a little on the inside...
    Every time I get shot.

  3. #3
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    thanks for the reply!
    does that mean it cannot be done with FileWriter?

    what are the pros/cons of BufferedWriter compared to FileWriter?

    Thanks again,

    Matt

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    from
    BufferedWriter (Java 2 Platform SE 5.0)

    Write text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.

    In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as FileWriters and OutputStreamWriters. For example,

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    With FileWriter you can use system properties as well.

    Java Code:
    String newLine = System.getProperty("line.separator");
    Then simply write this newLine once you write a line of text.

  6. #6
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    Thanks for that - it works brilliantly.

    Whilst I am at it, I need to actually make some of the fields a fixed length on the text file. Is this easy to acheive with the code I currently have (above)?

    thanks in advance,
    Matt

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Can you bit more clearly explain what you want to do here? Are you want to make the fixed length on the file text? What that file content, and how those data added to the file?

  8. #8
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Can you bit more clearly explain what you want to do here? Are you want to make the fixed length on the file text? What that file content, and how those data added to the file?


    Hi,
    in the code above, I am selecting 2 columns, and writing them to the file with a tab seperating them.
    The first of the two fields I am returning I want to make 32 characters (no matter how many come back from the database).
    this will result in the value being padded to the right by a certain number of spaces.

    for example, I currently get the following output:

    XML Code:
    INT5603BL	5000033
    INT5700BL	5000035
    TST14JAN	5000002
    INT5200BL	5000019
    INT5301BL	5000027
    INT6002BL	5000170
    INT4400BL	5000081
    INT4000BL	5000083
    INT1100BL	5000084

    this is just two fields seperated with a tab.

    I want to be able to say "give me the first field, but make sure the total length is 11 charaters, then give me the second field, making the output:
    XML Code:
    INT5603BL  5000033
    INT5700BL  5000035
    TST14JAN   5000002
    INT5200BL  5000019
    INT5301BL  5000027
    INT6002BL  5000170
    INT4400BL  5000081
    INT4000BL  5000083
    INT1100BL  5000084
    The actual end result will be several fields all set to specified lengths (in my spec)
    I have tried using:
    Java Code:
    fout.write(String.format("%11s",rs.getString(1)) + "\t"  + rs.getInt(2) + newLine);
    but this pads the value to the LEFT making the total number of characters (including the value returned from the database) 11. I need the trailing spaces to the right.

    Does this make sense?
    Regards,
    matt

  9. #9
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Yea, it does, for me. ;) You could, of course, write a simple method to pad it to the right, it probably also can be done with some method similar to the one you used now, but I wouldn't know.
    I die a little on the inside...
    Every time I get shot.

  10. #10
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Can you bit more clearly explain what you want to do here? Are you want to make the fixed length on the file text? What that file content, and how those data added to the file?
    Quote Originally Posted by Supamagier View Post
    Yea, it does, for me. ;) You could, of course, write a simple method to pad it to the right, it probably also can be done with some method similar to the one you used now, but I wouldn't know.
    Hi,
    well I have now found a method which seems to work ok:
    Java Code:
    public static String padRight(String s, int n) {
            return String.format("%1$-" + n + "s", s);
        }
    being extremeley wet behind the ears when it come sto Java, i'm not exactly sure I am doing things the right (or best) way.

    I've simply pasted that method above my Main method.
    Everything is in the one class at the moment.
    I'm sure it will work, but it seems a bit too amateur.
    I'm trying to think wether I need to split things out into other classes - but i'm not sure if I can or need to...

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Formatting is the best you have to use. And I don't think you need to go for a separate class to do this formatting. It's just need if you want to use your own formatting process other than JDK APIs.

  12. #12
    sanusi is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default to write newline in .txt

    I suggest, u can add "\r\n" into your source code

    original code
    fout.write(rs.getString(1) + "\t" + rs.getInt(2) + "\n");

    replace with
    fout.write(rs.getString(1) + "\t" + rs.getInt(2) + "\r\n");

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by sanusi View Post
    I suggest, u can add "\r\n" into your source code

    original code
    fout.write(rs.getString(1) + "\t" + rs.getInt(2) + "\n");

    replace with
    fout.write(rs.getString(1) + "\t" + rs.getInt(2) + "\r\n");
    And make your code work OK with Windows and not OK with Unix? No thank you. Eranga already suggested a better solution that is not OS-specific. Besides you're giving an incorrect answer to a thread that's over a year old.

    Locking.
    rajkapur likes this.

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

Similar Threads

  1. remove variables/line in a text file
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-04-2009, 03:05 AM
  2. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 10
    Last Post: 10-24-2008, 07:21 PM
  3. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 1
    Last Post: 10-24-2008, 12:31 AM
  4. saving to a new line a text
    By jadaleus in forum Advanced Java
    Replies: 1
    Last Post: 10-20-2008, 06:10 PM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 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
  •