Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataInputStream Help needed while creating file

 
naveen shrimal
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I am creating file using DataInputStream and entering primitive data types in the file named "a.txt" it shows some encoded text but when i read the same file through it displays output as expected ....
Here is the code...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DataOutputStream writes the values of variables and objects in a binary format, that you cannot read with a text editor or display in a console window as text. Don't save this data in a *.txt file, because it isn't text.

Ofcourse, if your program reads the file with a DataInputStream, it will be able to interpret the binary data in the file and display it correctly (the DataInputStream doesn't care that the file extension is "txt", even though it does not contain plain text).

Java has two kinds of I/O classes: InputStreams and OutputStreams, and Readers and Writers. The first are for reading and writing binary data. The second are for reading and writing text.

If you want to write data as text, use a Writer. For example a FileWriter.
 
naveen shrimal
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was too thinking about this but I want to make it sure. thanks for making it clear to me.
It is very good site.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic