• Post Reply Bookmark Topic Watch Topic
  • New Topic

byte doesn't copied to text file  RSS feed

 
vinoth vino
Ranch Hand
Posts: 67
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you so much 😊 Is FileOutputStream works with this?
 
Carey Brown
Saloon Keeper
Posts: 3324
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 56562
172
  • Mark post as helpful
  • send pies
  • 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: 56562
172
  • Mark post as helpful
  • send pies
  • 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: 56562
172
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!