• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Simple question - char  RSS feed

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

Why that doesn't work ?
 
Greenhorn
Posts: 19
  • 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

 
lowercase baba
Bartender
Posts: 12627
50
Chrome Java Linux
  • 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
  • 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)
 
Sheriff
Posts: 23713
50
Eclipse IDE Firefox Browser MySQL Database
  • 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
  • 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
Posts: 23713
50
Eclipse IDE Firefox Browser MySQL Database
  • 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
  • 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
Posts: 23713
50
Eclipse IDE Firefox Browser MySQL Database
  • 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
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When to use ObjectOutputStream?
does anybody know?
 
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!