Java FileOutputStream Write saveContentToFile(DataHandler content, File outFile)

Here you can find the source of saveContentToFile(DataHandler content, File outFile)

Description

Saves the specified content to the specified file

License

Open Source License

Parameter

Parameter Description
content The content
outFile The output file

Exception

Parameter Description
IOException If an error occurs during saving

Declaration

static public long saveContentToFile(DataHandler content, File outFile) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.activation.DataHandler;

public class Main {
    /**//from  w  w w .j  a v a  2  s .c o  m
     * Saves the specified content to the specified file
     * @param content The content
     * @param outFile The output file
     * @throws IOException If an error occurs during saving
     */
    static public long saveContentToFile(DataHandler content, File outFile) throws IOException {
        long size = 0;
        byte[] buffer = new byte[1024];
        try (InputStream is = content.getInputStream()) {
            try (OutputStream outStream = new FileOutputStream(outFile)) {
                for (int readBytes; (readBytes = is.read(buffer, 0, buffer.length)) > 0;) {
                    size += readBytes;
                    outStream.write(buffer, 0, readBytes);
                }
            }
        }
        return size;
    }
}

Related

  1. saveBytesToFile(byte[] data, File file)
  2. saveBytesToFile(byte[] str, File file)
  3. saveBytesToFile(String filepath, byte[] data)
  4. saveCertReqToFile(String certReq, String fileLocation)
  5. saveColorTable(final float[][] table, final File file)
  6. saveData(byte[] data, String PATH, String fileName)
  7. saveDataToFile(String filePath, byte[] data)
  8. saveDownloadedFile(InputStream is, String destination)
  9. saveEntry(String destPath, ZipEntry target, ZipFile zf)