Java FileOutputStream Write saveToFile(String baseName, String extension, byte[] content)

Here you can find the source of saveToFile(String baseName, String extension, byte[] content)

Description

Saves byte[] to new file.

License

Apache License

Parameter

Parameter Description
baseName name for file created (without extension), if a file already exists with the supplied name an '_index' will be added.
extension extension for file.
content data to store in file.

Return

absolute path of created file.

Declaration

public static String saveToFile(String baseName, String extension, byte[] content) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**//from   ww  w  .  j  av  a2s .  c  o m
     * Saves byte[] to new file.
     *
     * @param baseName name for file created (without extension),
     *                 if a file already exists with the supplied name an
     *                 '_index' will be added.
     * @param extension extension for file.
     * @param content data to store in file.
     * @return absolute path of created file.
     */
    public static String saveToFile(String baseName, String extension, byte[] content) {
        String result;
        File output = determineFilename(baseName, extension);
        FileOutputStream target = null;
        try {
            target = new FileOutputStream(output);
            target.write(content);
            result = output.getAbsolutePath();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (target != null) {
                try {
                    target.close();
                } catch (IOException ex) {
                    // what to to?
                }
            }
        }
        return result;
    }

    private static File determineFilename(String baseName, String extension) {
        File output = new File(baseName + "." + extension);
        // ensure directory exists
        File parent = output.getAbsoluteFile().getParentFile();
        if (!parent.exists()) {
            if (!parent.mkdirs()) {
                throw new IllegalArgumentException("Unable to create directory: " + parent.getAbsolutePath());
            }
        }
        int i = 0;
        // determine first filename not yet in use.
        while (output.exists()) {
            i++;
            String name = String.format("%s_%s.%s", baseName, i, extension);
            output = new File(name);
        }
        return output;
    }
}

Related

  1. saveToFile(final String file, final byte[] data, final boolean overwrite)
  2. saveToFile(final String text, final File file)
  3. saveToFile(InputStream in, File dst)
  4. saveToFile(InputStream in, String fileName)
  5. saveToFile(InputStream stream, File file)
  6. saveToFile(String data, File file)
  7. saveToFile(String data, File file)
  8. saveToFile(String fileName, String fileCode, File dir, String fileExtention)
  9. saveToFile(String filePath, InputStream in)