Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2008

Tip: Looking for answers? Try searching our database.

file output in java : Notepad gives weird results

Thread view: 
ankur - 30 Mar 2008 02:58 GMT
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 !?
Arne Vajhøj - 30 Mar 2008 03:03 GMT
> I used the following code to write output to a file.
> FileOutputStream fout = new FileOutputStream(fname,true);
[quoted text clipped - 36 lines]
>
> 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
Patricia Shanahan - 30 Mar 2008 03:49 GMT
>> I used the following code to write output to a file.
>> FileOutputStream fout = new FileOutputStream(fname,true);
[quoted text clipped - 39 lines]
> 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
JussiJ - 01 Apr 2008 04:23 GMT
> 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
Wayne - 30 Mar 2008 03:41 GMT
> I used the following code to write output to a file.
> FileOutputStream fout = new FileOutputStream(fname,true);
[quoted text clipped - 38 lines]
>
> 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
Chase Preuninger - 30 Mar 2008 19:35 GMT
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-software-develoupment?hl=en
Roedy Green - 30 Mar 2008 20:19 GMT
On Sat, 29 Mar 2008 18:58:41 -0700 (PDT), ankur
<ankur.a.agarwal@gmail.com> 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.
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.