Java Write Byte Array to File writeFile(byte[] data, String path, boolean overwrite)

Here you can find the source of writeFile(byte[] data, String path, boolean overwrite)

Description

Writes a file to the given path, appending digits to the end of the file name until it is unique.

License

Apache License

Parameter

Parameter Description
os The data input stream to write.
path The path to write to.

Return

The path of the file written to (which may be different than path, if a file at path already exists).

Declaration

public static String writeFile(byte[] data, String path, boolean overwrite) throws IOException 

Method Source Code


//package com.java2s;
/*//w w w  . j ava2s  .c  om
 * Copyright (C) 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;

public class Main {
    /**
     * Writes a file to the given path, appending digits to the end of the file name until it is unique.
     * @param os The data input stream to write.
     * @param path The path to write to.
     * @return The path of the file written to (which may be different than path, if a file at path already exists).
     */
    public static String writeFile(byte[] data, String path, boolean overwrite) throws IOException {
        OutputStream os = null;
        try {
            if (data == null)
                throw new IOException("Data is null");
            String path2 = path;
            File f = new File(path2);
            int index = 0;
            while (f.exists() && !overwrite) {
                index++;
                int ext = path.lastIndexOf(".");
                if (ext == -1)
                    ext = path.length();
                path2 = path.substring(0, ext) + Integer.toString(index) + path.substring(ext, path.length());
                f = new File(path2);
            }
            if (path.lastIndexOf(File.separator) > 0)
                createFolder(path.substring(0, path.lastIndexOf(File.separator)));
            f.createNewFile();
            System.out.println("Path: " + f.getAbsoluteFile());
            os = new FileOutputStream(f, false);
            //      int i;
            //      while ((i = is.read()) != -1)
            //        os.write(i);
            os.write(data);
            return path2;
        } finally {
            if (os != null)
                try {
                    os.close();
                } catch (IOException e) {
                }
        }
    }

    public static boolean createFolder(String path) {
        if (storageReady()) {
            boolean made = true;
            File dir = new File(path);
            if (!dir.exists()) {
                made = dir.mkdirs();
            }
            return made;
        } else {
            return false;
        }
    }

    private static boolean storageReady() {
        return true;
        //        String cardstatus = Environment.getExternalStorageState();
        //        if (cardstatus.equals(Environment.MEDIA_REMOVED)
        //                || cardstatus.equals(Environment.MEDIA_UNMOUNTABLE)
        //                || cardstatus.equals(Environment.MEDIA_UNMOUNTED)
        //                || cardstatus.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        //            return false;
        //        } else {
        //            return true;
        //        }
    }
}

Related

  1. writeFile(byte[] content, String filename)
  2. writeFile(byte[] data, File file)
  3. writeFile(byte[] data, File outFile)
  4. writeFile(byte[] data, String fileName)
  5. writeFile(byte[] data, String filePath)
  6. writeFile(byte[] fileData, String path)
  7. writeFile(byte[] outputBytes, String outputFile)
  8. writeFile(File f, byte[] data)
  9. writeFile(File file, byte[] b)