File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question - char

 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Why that doesn't work ?
 
Megha kharde
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Paul Clapham
Sheriff
Pie
Posts: 20188
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the whole code where "nothing happens"? Code fragments aren't very helpful.
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Pie
Posts: 20188
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you chose to view the output in Notepad, the worst text editor in the world?

yes
 
Paul Clapham
Sheriff
Pie
Posts: 20188
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i opened the same file in word the file displayed correct.

Thanks
 
Megha kharde
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When to use ObjectOutputStream?
does anybody know?
 
James Sabre
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic