file output in java : Notepad gives weird results

Discussion in 'Java' started by ankur, Mar 30, 2008.

  1. ankur

    ankur Guest

    I used the following code to write output to a file.
    FileOutputStream fout = new FileOutputStream(fname,true);
    PrintStream ps = new PrintStream(fout);
    Random r = new Random(10);
    for(int i = 0 ; i <= r1-1; i++)
    {
    for (int j = 0 ; j <= c1-1; j++)
    {
    m1[j] = r.nextInt(10);
    //System.out.println(m1[j]);
    ps.print(m1[j]);
    ps.printf("\n");
    }

    //ps.printf("\r");
    }
    ps.close();
    fout.close();
    }


    with this code if I read the output file in notepad I get all junk
    characters written out. Like this:

    ਲ਼ਰਲ਼ਰਸ਼ਸ਼

    But if I open this file in textpad the output is fine:

    3
    0
    3
    0
    6
    6

    However if I uncomment
    //ps.printf("\r");

    Output in notepad is surprising. There are no new lines: 303066

    But textpad is fine !


    Don't understand what is happening !?
     
    ankur, Mar 30, 2008
    #1
    1. Advertising

  2. ankur wrote:
    > I used the following code to write output to a file.
    > FileOutputStream fout = new FileOutputStream(fname,true);
    > PrintStream ps = new PrintStream(fout);
    > Random r = new Random(10);
    > for(int i = 0 ; i <= r1-1; i++)
    > {
    > for (int j = 0 ; j <= c1-1; j++)
    > {
    > m1[j] = r.nextInt(10);
    > //System.out.println(m1[j]);
    > ps.print(m1[j]);
    > ps.printf("\n");
    > }
    >
    > //ps.printf("\r");
    > }
    > ps.close();
    > fout.close();
    > }
    >
    >
    > with this code if I read the output file in notepad I get all junk
    > characters written out. Like this:
    >
    > ਲ਼ਰਲ਼ਰਸ਼ਸ਼
    >
    > But if I open this file in textpad the output is fine:
    >
    > 3
    > 0
    > 3
    > 0
    > 6
    > 6
    >
    > However if I uncomment
    > //ps.printf("\r");
    >
    > Output in notepad is surprising. There are no new lines: 303066
    >
    > But textpad is fine !


    Apparently TextPad considers \n a line break while NotePad only
    considers \r\n a line break.

    That explains the last phenomenon.

    The first could be due to UTF-8 versus ISO-8859-1 issues.

    You could try explicit specifying character set.

    Arne
     
    Arne Vajhøj, Mar 30, 2008
    #2
    1. Advertising

  3. ankur

    Wayne Guest

    ankur wrote:
    > I used the following code to write output to a file.
    > FileOutputStream fout = new FileOutputStream(fname,true);
    > PrintStream ps = new PrintStream(fout);
    > Random r = new Random(10);
    > for(int i = 0 ; i <= r1-1; i++)
    > {
    > for (int j = 0 ; j <= c1-1; j++)
    > {
    > m1[j] = r.nextInt(10);
    > //System.out.println(m1[j]);
    > ps.print(m1[j]);
    > ps.printf("\n");
    > }
    >
    > //ps.printf("\r");
    > }
    > ps.close();
    > fout.close();
    > }
    >
    >
    > with this code if I read the output file in notepad I get all junk
    > characters written out. Like this:
    >
    > ਲ਼ਰਲ਼ਰਸ਼ਸ਼
    >
    > But if I open this file in textpad the output is fine:
    >
    > 3
    > 0
    > 3
    > 0
    > 6
    > 6
    >
    > However if I uncomment
    > //ps.printf("\r");
    >
    > Output in notepad is surprising. There are no new lines: 303066
    >
    > But textpad is fine !
    >
    >
    > Don't understand what is happening !?


    You need to use a Writer class to translate the output to text
    your system understands. It is possible Textpad has some
    auto-detection feature for the file's encoding and is
    converting it automatically.

    -Wayne
     
    Wayne, Mar 30, 2008
    #3
  4. Arne Vajhøj wrote:
    > ankur wrote:
    >> I used the following code to write output to a file.
    >> FileOutputStream fout = new FileOutputStream(fname,true);
    >> PrintStream ps = new PrintStream(fout);
    >> Random r = new Random(10);
    >> for(int i = 0 ; i <= r1-1; i++)
    >> {
    >> for (int j = 0 ; j <= c1-1; j++)
    >> {
    >> m1[j] = r.nextInt(10);
    >> //System.out.println(m1[j]);
    >> ps.print(m1[j]);
    >> ps.printf("\n");
    >> }
    >>
    >> //ps.printf("\r");
    >> }
    >> ps.close();
    >> fout.close();
    >> }
    >>
    >>
    >> with this code if I read the output file in notepad I get all junk
    >> characters written out. Like this:
    >>
    >> ਲ਼ਰਲ਼ਰਸ਼ਸ਼
    >>
    >> But if I open this file in textpad the output is fine:
    >>
    >> 3
    >> 0
    >> 3
    >> 0
    >> 6
    >> 6
    >>
    >> However if I uncomment
    >> //ps.printf("\r");
    >>
    >> Output in notepad is surprising. There are no new lines: 303066
    >>
    >> But textpad is fine !

    >
    > Apparently TextPad considers \n a line break while NotePad only
    > considers \r\n a line break.

    ....

    Maybe NotePad is interpreting a \r followed by another character other
    than \n as a weird character?

    Note that the line termination problem can be fixed very easily by
    replacing 'ps.printf("\r");' with 'ps.printf("%n");'.

    Patricia
     
    Patricia Shanahan, Mar 30, 2008
    #4
  5. Chase Preuninger, Mar 30, 2008
    #5
  6. ankur

    Roedy Green Guest

    Roedy Green, Mar 30, 2008
    #6
  7. ankur

    JussiJ Guest

    On Mar 30, 1:03 pm, Arne Vajhøj <> wrote:

    > Apparently TextPad considers \n a line break while NotePad
    > only considers \r\n a line break.
    >
    > That explains the last phenomenon.


    What is happening is Windows defines the end of line as \r\n,
    where as Unix defines the end of line as a single \n.

    TextPad is detecting and reading the file as a Unix text file
    where as Notpad only understands Windows so it displays it as
    a single line because the line does not contain a Windows end
    of line marker.

    Jussi Jumppanen
    Author: Zeus for Windows IDE
    http://www.zeusedit.com
     
    JussiJ, Apr 1, 2008
    #7
    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. JDS
    Replies:
    4
    Views:
    8,384
    ForestHonerkamp
    Nov 11, 2008
  2. Replies:
    8
    Views:
    5,167
    Oliver Wong
    May 24, 2006
  3. boris
    Replies:
    1
    Views:
    1,912
  4. Ed Hardy
    Replies:
    12
    Views:
    290
    Robert Dober
    Nov 22, 2008
  5. JDS
    Replies:
    3
    Views:
    226
    gnari
    May 24, 2004
Loading...

Share This Page