[Logo]
Forums Register Login
Simple question - char

Why that doesn't work ?
FileOutputStream f=new FileOutputStream("out.ser");
f.write();

Here, this write() method argument is of type int, so Unicode character value of '\n' i.e 10 is written to the file .
but if i use ObjectOutputStream and use its writeChar(char) method instead of FileOutOutStream newline character is writene to the file .


In my existing serializable Car class i tried to use ObjectOutputStream and its writechar(char) method

 
Dawid Skrzypczynski wrote:
Why that doesn't work ?

Can you define what exactly you mean by "doesn't work"? Doesn't compile? Doesn't run? runs but gives weird output?
I want to add new line to the file but if i write
char x = '\n';
fileOutputStream.write(x);
that nothing happens. I tried this way but it also nothing happens
...write((char)10)
Can you post the whole code where "nothing happens"? Code fragments aren't very helpful.
The example is really simple. I am reading something to scanner and i am writing this to the file as long chain will not be equals "stop". after when i read whole string i want to add new line i add char x but nothing is added.

really sorry for my english.
 
Dawid Skrzypczynski wrote:The example is really simple.
Simple but wrong. You don't close the FileOutputStream anywhere.

It's also worth asking at this point what makes you think that the \n character isn't written out. Perhaps you chose to view the output in Notepad, the worst text editor in the world?
 
Perhaps you chose to view the output in Notepad, the worst text editor in the world?

yes
 
Dawid Skrzypczynski wrote:
Perhaps you chose to view the output in Notepad, the worst text editor in the world?

yes


Aha. Notepad is so Windows-oriented it doesn't work right unless the file has Windows line endings (\r\n). But don't change your code to do that -- just use almost any other text editor. Even Wordpad gets it right, I think.

(Simple question: Not simple answer.)
When i opened the same file in word the file displayed correct.

Thanks
When to use ObjectOutputStream?
does anybody know?
 
Megha kharde wrote:When to use ObjectOutputStream?
does anybody know?


When you want to write a Java object (or a graph of Java objects) by serializing them!
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 1467 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 18, 2017 13:46:25.