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..
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:
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]
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.)
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?
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.
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: