File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Simple question - char 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 » Java in General
Bookmark "Simple question - char" Watch "Simple question - char" New topic
Author

Simple question - char

Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52

Why that doesn't work ?
Megha kharde
Greenhorn

Joined: Nov 24, 2010
Posts: 19
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

Joined: Oct 02, 2003
Posts: 11404
    
  16

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?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
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
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

Can you post the whole code where "nothing happens"? Code fragments aren't very helpful.
Dawid Skrzypczynski
Ranch Hand

Joined: Jun 08, 2011
Posts: 52
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
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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

Joined: Jun 08, 2011
Posts: 52
Perhaps you chose to view the output in Notepad, the worst text editor in the world?

yes
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

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

Joined: Jun 08, 2011
Posts: 52
When i opened the same file in word the file displayed correct.

Thanks
Megha kharde
Greenhorn

Joined: Nov 24, 2010
Posts: 19
When to use ObjectOutputStream?
does anybody know?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

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!


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple question - char