Java Utililty Methods FileOutputStream Create

List of utility methods to do FileOutputStream Create

Description

The list of methods to do FileOutputStream Create are organized into topic(s).

Method

booleanwriteFileFromString(File outputFileName, String content)
ouputs a new file
try {
    FileOutputStream fos2 = new FileOutputStream(outputFileName, false);
    fos2.write(content.getBytes());
    fos2.close();
} catch (IOException ioe) {
    return false;
return true;
...
voidwriteFileRaw(String fileName, byte[][] contents)
Method to write raw content to the file.
try (OutputStream output = new BufferedOutputStream(new FileOutputStream(fileName))) {
    for (byte[] content : contents) {
        output.write(content);
    output.flush();
voidwriteFileToByte(byte[] bytes, File file)
write File To Byte
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(bytes);
fileOutputStream.close();
BooleanwriteFileToDisk(ByteArrayOutputStream is, String savePath)
write File To Disk
try {
    OutputStream outputStream = new FileOutputStream(savePath);
    is.writeTo(outputStream);
} catch (Exception e) {
    return false;
return true;
voidwriteFileToDisk(File file, String targetPath)
write File To Disk
FileInputStream fis = null;
FileOutputStream fos = null;
try {
    fis = new FileInputStream(file);
    fos = new FileOutputStream(targetPath);
    byte[] b = new byte[1];
    while (fis.read(b) != -1) {
        fos.write(b);
...
voidwriteFileToFile(File inFile, File toFile)
write File To File
OutputStream os = null;
InputStream is = null;
try {
    is = new FileInputStream(inFile);
    os = new FileOutputStream(toFile);
    byte[] buffer = new byte[4096];
    for (int n; (n = is.read(buffer)) != -1;) {
        os.write(buffer, 0, n);
...
voidwriteFileToOutputStream(ZipInputStream zipInputStream, File outputFile)
Writes a ZipInputStream to a File.
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
try {
    writeInputStreamToOutputStream(zipInputStream, fileOutputStream);
} finally {
    try {
        fileOutputStream.close();
    } catch (Throwable ignore) {
voidwriteFileUTF8(final File file, final String contents)
Write the contents of the string out to a file in UTF-8 format.
final Writer writer = new OutputStreamWriter(new FileOutputStream(file), UTF8);
try {
    writer.write(contents);
} finally {
    writer.close();
voidwriteFileWithParent(File file, String contentStr, String charset)
write File With Parent
write(file, contentStr, charset);
voidwriteFileWithParent(String filename, byte[] content)
write File With Parent
File file = new File(filename);
File parent = file.getParentFile();
if (!parent.exists()) {
    parent.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
...