public static void appendFileToStream(File file, OutputStream outputStream) throws IOException { if (file == null) { throw new IllegalArgumentException("'file' is null"); } if (!file.exists()) { throw new FileNotFoundException("File does not exist: " + file); } if (file.isDirectory()) { throw new FileNotFoundException("File is directory: " + file); } FileInputStream input = null; try { input = new FileInputStream(file); copy(input, outputStream); } finally { ensureClosed(input); } }
public static void writeBytes(File file, byte[] bytes) throws IOException { if (file == null) { throw new IllegalArgumentException("'file' is null"); } if (bytes == null) { throw new IllegalArgumentException("'bytes' is null"); } File parentFile = file.getParentFile(); if (!parentFile.exists() && !parentFile.mkdirs()) { throw new IOException("Parent file could not be created: " + parentFile); } ByteArrayInputStream input = null; FileOutputStream output = null; try { input = new ByteArrayInputStream(bytes); output = new FileOutputStream(file); copy(input, output); } finally { ensureClosed(input); ensureClosed(output); } }