Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

writing a file onto an outputStream  RSS feed

 
Neil Tomlin
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm struggling to work out if there is a way that I can write a whole file onto an output stream.
I've already checked the file definitely exists, but it may be .html, .jpeg, .mp3 etc.

Is it possible to just put



theFile onto an outputstream without trying to read it?

Thanks
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neil Tomlin wrote:
theFile onto an outputstream without trying to read it?


No. If you haven't read the file, you don't have anything to write.
You can use the File class' rename method to move a file. The closest thing to what you want to do is probably to use Channels:



There are many other ways.
 
Neil Tomlin
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, managed to work out that reading it would be the best thing to do. What I didn't know was that I needed to read the file as bytes. Eventually got it though. I did:



 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's fine, but slow. You should read and write more than one byte at a time, either by using the read(byte[] b) and write(byte [] b) methods, or by using BufferedInputStream and BufferedOutputStream. (Remember to flush!)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!