Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
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
Devaka Cooray
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Jeanne Boyarsky
Tim Cooke
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Mikalai Zaikin
Carey Brown
Bartenders:
Forum:
Java in General
Saving a Inputstream to file
Alex Philippi
Greenhorn
Posts: 13
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I am saving a inputstream to a temp File (for accessing the stream more than once). But I think thats a kind of "ugly" code. Is there a better way to implement this?
Here the code:
public class TempXmlFile { private File tempFile; public TempXmlFile(InputStream in) throws IOException { this.init(in); } private void init(InputStream in) throws IOException { tempFile = File.createTempFile("tempFile", ".tmp"); tempFile.deleteOnExit(); FileOutputStream fout = null; try { fout = new FileOutputStream(tempFile); int c; while ((c = in.read()) != -1) { fout.write(c); } }finally { if (in != null) { in.close(); } if (fout != null) { fout.close(); } } } public File getTempFile() { return tempFile; } }
Best Regards
Alex
Rob Spoor
Sheriff
Posts: 22772
130
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You can gain some performance speed by using buffers for copying. So instead of
int c; while ((c = in.read()) != -1) { fout.write(c); }
use
byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) != -1) { fout.write(buf, 0, len); }
Varying the buffer size can improve speed even more.
Also try wrapping the reader in a BufferedReader and read from that.
Perform multiple tests to see which one is best for you.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions
How To Answer Questions
A magnificient life is loaded with tough challenges. En garde tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
problem opening a new word file
File upload in struts
Drag and Drop to Desktop Issue
Untar a tar.gz file
PDF with jsf
More...