Java Utililty Methods InputStream Copy to File

List of utility methods to do InputStream Copy to File

Description

The list of methods to do InputStream Copy to File are organized into topic(s).

Method

voidcopyStream(final InputStream in, final File dest)
Copies the content of the InputStream in to the File dest.
FileOutputStream out = new FileOutputStream(dest);
BufferedInputStream bin = new BufferedInputStream(in);
try {
    byte[] buffer = new byte[BUFFER_SIZE];
    int len;
    while ((len = bin.read(buffer, 0, 2048)) != -1) {
        out.write(buffer, 0, len);
    out.close();
    bin.close();
} catch (IOException ex) {
    out.close();
    bin.close();
    throw ex;
longcopyStream(final InputStream is, final File destinationFile)
Downloads to a temporary location and renames on finish so no partials
final File tmpFile = File.createTempFile("flickr_download", null);
try (final BufferedInputStream bis = new BufferedInputStream(is, BUF_SIZE);
        final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile),
                BUF_SIZE);) {
    final long total = ByteStreams.copy(bis, bos);
    tmpFile.renameTo(destinationFile);
    return total;
voidcopyStream(InputStream copyFrom, File copyTo)
Copy an input stream to a file location
OutputStream to = new FileOutputStream(copyTo);
copyStream(copyFrom, to);
voidcopyStream(InputStream in, File dest)
copy Stream
FileOutputStream out = new FileOutputStream(dest);
BufferedInputStream bin = new BufferedInputStream(in);
byte[] buffer = new byte[2048];
int len;
while ((len = bin.read(buffer, 0, 2048)) != -1) {
    out.write(buffer, 0, len);
out.close();
...
booleancopyStream(InputStream in, File dest)
Copy a file content to another location.
try {
    OutputStream out = new FileOutputStream(dest);
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    in.close();
...
voidcopyStreamIntoFile(File outFile, InputStream is)
copy Stream Into File
OutputStream os = new FileOutputStream(outFile);
byte[] buf = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
    os.write(buf, 0, len);
is.close();
os.close();
...
voidcopyStreamsToFile(String path, Map streamMap)
copy Streams To File
for (String copyBookName : streamMap.keySet()) {
    File copyBookFile = new File(path + copyBookName);
    OutputStream copyBookStream = new FileOutputStream(copyBookFile);
    OutputStreamWriter copyBookStreamWriter = new OutputStreamWriter(copyBookStream);
    copyBookFile.deleteOnExit();
    BufferedReader input = new BufferedReader(new InputStreamReader(streamMap.get(copyBookName)));
    String line;
    while ((line = input.readLine()) != null) {
...
voidcopyStreamsToFolder(Iterator> streams, File folder)
copy Streams To Folder
while (streams.hasNext()) {
    Map.Entry<String, InputStream> entry = streams.next();
    File file = new File(folder, entry.getKey());
    copyStreamToFile(entry.getValue(), file);
voidcopyStreamToFile(final File to, final InputStream from)
Copy the contents of a stream to the given file.
if (!to.exists()) {
    to.createNewFile();
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(to));
copyStream(writer, from);
writer.close();
voidcopyStreamToFile(InputStream from, File to)
copy Stream To File
to.getParentFile().mkdirs();
OutputStream out = new BufferedOutputStream(new FileOutputStream(to));
try {
    copyStream(from, out);
} finally {
    out.close();