Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Write a program in notepad

 
Amey Ambulgekar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1557
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48981
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we all came to that conclusion eventually, but Anayonkar Shivalkar was first.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic