Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > file output in java : Notepad gives weird results

Reply
Thread Tools

file output in java : Notepad gives weird results

 
 
ankur
Guest
Posts: n/a
 
      03-30-2008
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[i][j] = r.nextInt(10);
//System.out.println(m1[i][j]);
ps.print(m1[i][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 !?



 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      03-30-2008
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[i][j] = r.nextInt(10);
> //System.out.println(m1[i][j]);
> ps.print(m1[i][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

 
Reply With Quote
 
 
 
 
Wayne
Guest
Posts: n/a
 
      03-30-2008
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[i][j] = r.nextInt(10);
> //System.out.println(m1[i][j]);
> ps.print(m1[i][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
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      03-30-2008
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[i][j] = r.nextInt(10);
>> //System.out.println(m1[i][j]);
>> ps.print(m1[i][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
 
Reply With Quote
 
Chase Preuninger
Guest
Posts: n/a
 
      03-30-2008
Your character encoding sounds like it is all messed up. Also the
newline character on most windows machines is \r\n.

http://groups.google.com/group/java-...loupment?hl=en
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      03-30-2008
On Sat, 29 Mar 2008 18:58:41 -0700 (PDT), ankur
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

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


See http://mindprod.com/jgloss/encoding.html
see http://mindprod.com/jgloss/hex.html

you want to view the file in hex to see just what is in there.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
JussiJ
Guest
Posts: n/a
 
      04-01-2008
On Mar 30, 1:03*pm, Arne Vajhj <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting weird error trying to notepad a file Lynn McGuire Windows 64bit 5 02-04-2011 11:11 PM
Notepad++ (no debug output, using XP) Ed Hardy Ruby 12 11-22-2008 10:24 AM
cant open notepad, gives application error. im_prad General Computer Support 0 08-02-2008 07:36 PM
how to use io operations with open/save file dialog box in file menu for simple notepad boris Java 1 07-22-2007 04:13 PM
Spawning process from Java gives deadlock for standard output peter.koch.larsen@gmail.com Java 8 05-24-2006 08:36 PM



Advertisments