• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

byte doesn't copied to text file

 
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heloooooo Programmers. I've coded for copying the strings into text files using FileOutputStreams

CODE


Here,I'll explain what i did... First,I get the bytes of strings and then assigned to ByteArrayInputStream.Then i list those strings to the console window,then I copied this to files using FileInputStream but I've only the empty text file. There were no contents in that file. Please anyone help me out.. Thanks in advance
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Output streams are not intended for text files. Use a file writer, print writer, formatter or similar for text files. And make sure to close the writer, otherwise everything might stay in the buffer for ever and you won't see it.
 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much 😊 Is FileOutputStream works with this?
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need something more like:

Don't use bytes for text files, a byte will not hold a single character (which is 2 bytes long).
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinoth vino wrote:Thank you so much 😊

You're welcome

Is FileOutputStream works with this?

No.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The classes you want for text files have names ending Reader or Writer. Though you may be better off with the well‑known Formatter and Scanner classes. ReadAllAboutIt in the Javaâ„¢ Tutorials.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote:. . . ReadAllAboutIt . . .

That is what the chaps who used to stand in the middle of the High Street said, as they came to your car window to sell the Evening Standard, when I was little.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

vinoth vino wrote:...then I copied this to files using FileInputStream but I've only the empty text file. There were no contents in that file. Please anyone help me out.



Well, let's look at your code.



Line 29. You created an output file, which starts off as an empty file.

Line 30. You created an byte array output stream, which starts off as an empty byte array.

line 31. You get the byte array, which is an empty byte array.

line 33. You write the empty byte array into the byte array stream, which means that it is still an empty byte array.

line 34. You write the empty byte array stream into the file, which means that it is still an empty file.

The output looks like it is correct to me.
Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic