• 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

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: 76395
364
  • 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: 9539
79
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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: 76395
364
  • 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: 76395
364
  • 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: 76395
364
  • 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: 23928
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
 
In the renaissance, how big were the dinosaurs? Did you have tiny ads?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic