output to a file: newline character \n

Discussion in 'Java' started by 3rdshiftcoder, Apr 16, 2007.

  1. hi-

    everytime i try to print out a newline character with writeBytes i get a
    funny symbol
    instead of a new line in my text file. what am i missing? i use java 1.5x

    thanks for any assistance,
    jim

    package demolib;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.hibernate.*;
    import java.util.List;
    import java.util.Iterator;
    import java.io.*;
    public class ResetVendor {
    public static void write(DataOutputStream outStream, String text) {
    try {
    outStream.writeBytes(text +" "+ "\n");
    }
    catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    public static void main(String[] args)throws IOException{
    Transaction tx = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    DataOutputStream outFile = new DataOutputStream(new
    FileOutputStream("file4.txt"));
    try {
    tx = session.beginTransaction();
    Query query = session.createQuery("from Payeeor");
    List books = query.list();
    for (Iterator iter = books.iterator(); iter.hasNext();) {
    Payeeor vendor = (Payeeor) iter.next();
    String s = vendor.getPayeepayor();
    write(outFile,s);
    //session.saveOrUpdate(vendor);
    }
    tx.commit();
    } catch (HibernateException e) {
    if (tx != null) tx.rollback();
    throw e;
    } finally {
    session.close();
    outFile.close();
    }
    }
    }
     
    3rdshiftcoder, Apr 16, 2007
    #1
    1. Advertising

  2. hi-
    i found that if i use \r\n it works.
    that is strangely peculiar.
    i will have to investigate this \r business.

    thanks,
    jim
     
    3rdshiftcoder, Apr 16, 2007
    #2
    1. Advertising

  3. 3rdshiftcoder wrote:
    ....
    >i found that if i use \r\n it works.
    >that is strangely peculiar.
    >i will have to investigate this \r business.


    Investigate System.getProperty("line.separator"). This
    produces the correct line separator for each platform.
    Also investigate using your shift key once at the
    start of each sentence, to assist the reader.

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200704/1
     
    Andrew Thompson, Apr 16, 2007
    #3
  4. 3rdshiftcoder

    Z. Guest

    3rdshiftcoder wrote:
    > hi-
    > i found that if i use \r\n it works.
    > that is strangely peculiar.
    > i will have to investigate this \r business.


    Have you looked at System.getProperty("line.separator") ?
     
    Z., Apr 16, 2007
    #4
  5. >>System.getProperty("line.separator")

    Ok.
    I will check out that method call.
    Thanks very much for the tip.

    jim
     
    3rdshiftcoder, Apr 16, 2007
    #5
  6. 3rdshiftcoder

    Karl Uppiano Guest

    "3rdshiftcoder" <> wrote in message
    news:4622ed01$0$19399$...
    >>>System.getProperty("line.separator")

    >
    > Ok.
    > I will check out that method call.
    > Thanks very much for the tip.


    In addition, DataOutputStream is primarily intended for store and restore
    operations, going round trip from live Java object, to file, back to live
    Java object. The file written is not necessarily intended for external
    consumption. The fact that you can open it in a text editor is
    implementation dependent.

    If you want to write strings to a readable text file, it might be better to
    use something that gives you control of the output encoding, e.g., UTF-8,
    ISO-5988, etc., using an OutputStreamWriter on a FileOutputStream.
     
    Karl Uppiano, Apr 16, 2007
    #6
  7. "Karl Uppiano" <> wrote in message
    news:1qEUh.572$Da6.34@trnddc02...
    >
    > "3rdshiftcoder" <> wrote in message
    > news:4622ed01$0$19399$...
    >>>>System.getProperty("line.separator")

    >>
    >> Ok.
    >> I will check out that method call.
    >> Thanks very much for the tip.

    >
    > In addition, DataOutputStream is primarily intended for store and restore
    > operations, going round trip from live Java object, to file, back to live
    > Java object. The file written is not necessarily intended for external
    > consumption. The fact that you can open it in a text editor is
    > implementation dependent.
    >
    > If you want to write strings to a readable text file, it might be better
    > to use something that gives you control of the output encoding, e.g.,
    > UTF-8, ISO-5988, etc., using an OutputStreamWriter on a FileOutputStream.


    Or better yet a PrintWriter, which allows you to use println(), which will
    figure out the proper line terminator for you.
     
    Mike Schilling, Apr 16, 2007
    #7
  8. 3rdshiftcoder

    Oliver Wong Guest

    "3rdshiftcoder" <> wrote in message
    news:4622decf$0$27062$...
    > hi-
    > i found that if i use \r\n it works.
    > that is strangely peculiar.
    > i will have to investigate this \r business.


    Read http://en.wikipedia.org/wiki/Newline

    - Oliver
     
    Oliver Wong, Apr 16, 2007
    #8
  9. "3rdshiftcoder" <> wrote in news:4622db85$0$8960
    $:

    > everytime i try to print out a newline character with writeBytes i get a
    > funny symbol


    In Java, char != byte. Try writing characters with a character-oriented
    OutputStream instead of using a byte-oriented Writer.

    Cheers

    --
    ---------------------------------------------------------------------
    Greg R. Broderick

    A. Top posters.
    Q. What is the most annoying thing on Usenet?
    ---------------------------------------------------------------------
     
    Greg R. Broderick, Apr 16, 2007
    #9
  10. 3rdshiftcoder

    Lew Guest

    Greg R. Broderick wrote:
    > "3rdshiftcoder" <> wrote in news:4622db85$0$8960
    > $:
    >
    >> everytime i try to print out a newline character with writeBytes i get a
    >> funny symbol

    >
    > In Java, char != byte. Try writing characters with a character-oriented
    > OutputStream instead of using a byte-oriented Writer.


    That would be a char-oriented Writer instead of a byte-oriented OutputStream.

    --
    Lew
     
    Lew, Apr 17, 2007
    #10
  11. Lew <> wrote in
    news::

    > Greg R. Broderick wrote:
    >> "3rdshiftcoder" <> wrote in news:4622db85$0$8960
    >> $:
    >>
    >>> everytime i try to print out a newline character with writeBytes i get
    >>> a funny symbol

    >>
    >> In Java, char != byte. Try writing characters with a
    >> character-oriented OutputStream instead of using a byte-oriented
    >> Writer.

    >
    > That would be a char-oriented Writer instead of a byte-oriented
    > OutputStream.


    Thanks! Sometimes you've got to tell me "no, no, the OTHER right", after you
    tell me to take a right at the next light, too ;-)

    --
    ---------------------------------------------------------------------
    Greg R. Broderick

    A. Top posters.
    Q. What is the most annoying thing on Usenet?
    ---------------------------------------------------------------------
     
    Greg R. Broderick, Apr 17, 2007
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dragos Chirila

    NEWLINE character problem

    Dragos Chirila, Jan 30, 2004, in forum: Python
    Replies:
    1
    Views:
    6,937
    Nuff Said
    Jan 30, 2004
  2. Daniel Mark
    Replies:
    6
    Views:
    70,781
    Tim Chase
    Sep 28, 2006
  3. Replies:
    16
    Views:
    2,362
    Keith Thompson
    Nov 28, 2006
  4. Replies:
    6
    Views:
    425
  5. utab
    Replies:
    1
    Views:
    289
Loading...

Share This Page