aspose file tools*
The moose likes Beginning Java and the fly likes Write a  program in notepad Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Write a  program in notepad " Watch "Write a  program in notepad " New topic
Author

Write a program in notepad

Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
hello to all,

i have created a program using FileInputStream,DataOutputStream..etc for a file..

in that program i have created a notepad in 'E:\' directory by calling FileOutputStream fo = new FileOutputStream("E:\My_FileOutput.txt");

and it runs in well manner when i write it on console and write "ok" then it will stop taking input , & will automatically imprinted in to My_FileOutput but there is one problem i am not getting output on separate line ... if i write on console..

Hello <--line1
to all <--line2
ok <--line 3 after this separate notepad of My_FileOutput.txt created and output will be of "Helloto all ", but i want it on separate line... means:

Hello
to all in separate notepad

please help me out ... this is my program(and change the file path):



Thanking to all ... (lets hope my Q is not big to all )

[Edit - added code tags - see UseCodeTags]
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi Amey,

Amey Ambulgekar wrote: Hello
to all in separate notepad


What do you mean by 'separate notepad'? Or did you want to say separate line (I think so)?

When you read a line from console by readLine(), it simply returns
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

(from official documentation).

Next, you are not explicitly writing newline character to the file. All you are doing is : writing all elements of String array one after another. I think you got what's the problem here

(you could add new line character to all Strings while reading etc.)


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

You said "Notepad"? Unlike every other text editor ever written, Notepad only recognizes new-line followed by carriage-return as line endings. And that isn't what you are sending as line endings. So change your program if there is somebody standing behind you with a cricket bat who will whack you if you try to use a different text editor. Otherwise, use a different text editor.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1509
    
    5

Hi Paul,

Yes, you are right, that Notepad has this \n and \n\r issue, but I think this problem is with the code, not with the editor.

Since String read by readLine won't end with new line character, and since Amey is not explicitly adding new line character, but writing an array of String to the file, I think file will look same; no matter which editor is used here, right?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Why are you using Streams rather than Readers? Are you dealing with text files? Readers work better than Streams for text files.
Why have you got a 1000-member array? You don’t need to keep those Strings in memory if you are simply writing them to a file. What you want is a writing method; you can read the line and write it to the other file in byte[] format from inside the same loop.
You can get rid of the break, which I consider awkward, like thisWhy have you got the char for the linefeed character? You appear not to use it. Why did you call it str1?
Why are you not closing your streams? You ought to close them in a finally, or use try-with-resources, which only works in Java7.

If you are simply writing the String as a byte[], you are not adding line-end characters, so you will get the output as one continuous line.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

Anayonkar Shivalkar wrote:Since String read by readLine won't end with new line character, and since Amey is not explicitly adding new line character, but writing an array of String to the file, I think file will look same; no matter which editor is used here, right?


Yes, after looking at the code I think you're correct. I was misled by this line:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
I think we all came to that conclusion eventually, but Anayonkar Shivalkar was first.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write a program in notepad