• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Junk data written into file when write() is used

 
Greenhorn
Posts: 2
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I am trying to learn File I/O in Java. The following code writes some junk data into my text file instead of writing a byte array.



I tried searching for solutions on the Internet, couldnt get one anywhere.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OutputStream writes binary data and your byte array is an array of binary bytes. If you examine your file hi.txt using a hex editor you will see your bytes (in hex not decimal). If you want to write text then you need to use one of the java.io.Writer classes. In this case you need to wrap your OutputStream in an OutputStreamWriter and you may want to wrap this in a BufferedWriter or a PrintWriter so as to get a more comprehensive set of methods.
 
mano vishnu
Greenhorn
Posts: 2
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Sabre wrote:OutputStream writes binary data and your byte array is an array of binary bytes. If you examine your file hi.txt using a hex editor you will see your bytes (in hex not decimal). If you want to write text then you need to use one of the java.io.Writer classes. In this case you need to wrap your OutputStream in an OutputStreamWriter and you may want to wrap this in a BufferedWriter or a PrintWriter so as to get a more comprehensive set of methods.




The above code works but when I try to write an integer array into the file it gets stored as the same junk data.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
write() is for writing bytes as binary. print(), println() and printf() are for writing text. The Javadoc gives the details.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic