Android File Create createFile(String fileName)

Here you can find the source of createFile(String fileName)

Description

Create a FileOutputStream corresponding to a particular file name.

License

Open Source License

Declaration

public static final FileOutputStream createFile(String fileName)
        throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*w  w  w . j a  va 2  s.  com*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    /**
     * Create a {@link FileOutputStream} corresponding to a particular file name.
     * Delete the existing file if one exists.
     */
    public static final FileOutputStream createFile(String fileName)
            throws IOException {
        if (fileName == null) {
            throw new IllegalArgumentException("null file");
        }
        File f = new File(fileName);
        if (f.getParentFile() != null && !f.getParentFile().exists()) {
            boolean result = f.getParentFile().mkdirs();
            if (!result) {
                throw new IOException("failed to create "
                        + f.getParentFile());
            }
        }
        if (f.exists()) {
            f.delete();
        }
        boolean result = f.createNewFile();
        if (!result) {
            throw new IOException("failed to create " + f);
        }
        return new FileOutputStream(f);
    }
}

Related

  1. createChecksum(String path)
  2. createFile(File file, boolean isFile)
  3. createFile(String file, String content, String encodType)
  4. createFile(String fileName)
  5. createFile(String filePathAndName, String fileContent)
  6. createFile(String path)
  7. createFile(String path)
  8. createFile(String path, List content)