Example usage for java.io File createNewFile

List of usage examples for java.io File createNewFile

Introduction

In this page you can find the example usage for java.io File createNewFile.

Prototype

public boolean createNewFile() throws IOException 

Source Link

Document

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

Usage

From source file:Main.java

public static boolean copyAssetFile(Context context, String originFileName, String destFilePath,
        String destFileName) {//w w w  .  j av a2 s.co  m
    InputStream is = null;
    BufferedOutputStream bos = null;
    try {
        is = context.getAssets().open(originFileName);
        File destPathFile = new File(destFilePath);
        if (!destPathFile.exists()) {
            destPathFile.mkdirs();
        }

        File destFile = new File(destFilePath + File.separator + destFileName);
        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(destFile);
        bos = new BufferedOutputStream(fos);

        byte[] buffer = new byte[256];
        int length = 0;
        while ((length = is.read(buffer)) > 0) {
            bos.write(buffer, 0, length);
        }
        bos.flush();

        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (null != bos) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return false;
}

From source file:de.xirp.report.ReportManager.java

/**
 * This method opens the contained PDF data of the 
 * given {@link de.xirp.report.ReportDescriptor}
 * in the default viewer of the underlying operating system.
 * //from ww w.  ja v a 2 s  . c  om
 * @param rd
 *          The report descriptor to open.
 */
public static void viewReport(ReportDescriptor rd) {
    File tmp = new File(Constants.TMP_DIR, "report_temp_xirp2.pdf"); //$NON-NLS-1$
    try {
        tmp.createNewFile();
        FileUtils.writeByteArrayToFile(tmp, rd.getPdfData());
        SWTUtil.openFile(tmp);
    } catch (IOException e) {
        logClass.error("Error: " + e.getMessage() + Constants.LINE_SEPARATOR, e); //$NON-NLS-1$
    }
    if (tmp != null) {
        DeleteManager.deleteOnShutdown(tmp);
    }
}

From source file:Main.java

public static void createDipPath(String file) {
    String parentFile = file.substring(0, file.lastIndexOf("/"));
    File file1 = new File(file);
    File parent = new File(parentFile);
    if (!file1.exists()) {
        parent.mkdirs();// w  ww  .java2s  .  c  o m
        try {
            file1.createNewFile();
        } catch (IOException e) {
        }
    }
}

From source file:Main.java

public static String catchStreamToFile(String catchFile, InputStream inStream) throws Exception {

    File tempFile = new File(catchFile);
    try {//from   www .  j  av a  2  s.c  o m
        if (tempFile.exists()) {
            tempFile.delete();
        }
        tempFile.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
    byte[] buffer = new byte[1024];
    int len = 0;
    while ((len = inStream.read(buffer)) != -1) {
        fileOutputStream.write(buffer, 0, len);
    }
    inStream.close();
    fileOutputStream.close();
    return catchFile;
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.getParentFile().exists())
        destFile.getParentFile().mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();
    } else {/*from   w w w  .ja va2 s .c o  m*/
        destFile.delete();
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

public static void savePic(File targetFile, Bitmap bitmap) throws IOException {

    if (!targetFile.getParentFile().exists()) {
        targetFile.getParentFile().mkdirs();
    } else if (!targetFile.exists()) {
        targetFile.createNewFile();
    } else {/*from   w  w w. ja v a2 s.  c om*/
        FileOutputStream fos = new FileOutputStream(targetFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    }
}

From source file:com.bah.applefox.main.plugins.pageranking.utilities.PRtoFile.java

public static boolean writeToFile(String[] args) {
    String fileName = args[16];//from w  ww  . j a  v a2s .  com
    File f = new File(fileName);
    try {
        f.createNewFile();
        OutputStream file = new FileOutputStream(f);
        OutputStream buffer = new BufferedOutputStream(file);
        ObjectOutput out = new ObjectOutputStream(buffer);
        out.writeObject(createMap(args));
        out.flush();
        out.close();
    } catch (IOException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (AccumuloException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (AccumuloSecurityException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    } catch (TableNotFoundException e) {
        if (e.getMessage() != null) {
            log.error(e.getMessage());
        } else {
            log.error(e.getStackTrace());
        }
        return false;
    }

    return true;

}

From source file:com.dbmojo.Util.java

/** Check if the passed path exists. If parameter create is true
 *  then create the path is it is non existent.
 *///from www . j  a  va  2  s  .  c o  m
public static boolean pathExists(String path, boolean create) {
    File tFile = new File(path);
    if (create) {
        try {
            tFile.createNewFile();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
    return tFile.exists();
}

From source file:Main.java

public static boolean createDirIfNotExisted(String path, boolean isNomedia) {
    File file = new File(path);
    File fileName = new File(path + "/.nomedia");
    try {/*from w w  w  .  j  a v  a2s  . c o m*/
        if (!file.exists()) {
            file.mkdirs();
        }
        if (isNomedia && !fileName.exists()) {
            fileName.createNewFile();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:me.andrewpetersen.matrixuhc.api.storage.MatrixConfig.java

/**
 * Create a {@link YamlConfiguration} instance for a specified resource file name.
 *
 * @param resourceName The name of the file in the resources folder.
 * @return The YamlConfiguration file./*from w w w  . j  a  v  a 2 s.c  o  m*/
 */
public static MatrixConfig createConfiguration(String resourceName) throws IOException {
    Validate.notNull(resourceName, "Can't create a config with the resource name being null!");
    Validate.notNull(MatrixUHC.getInstance().getResource(resourceName),
            "No resources named \"" + resourceName + "\" were found! ");
    File dataFolder = MatrixUHC.getInstance().getDataFolder();
    if (!dataFolder.exists() && !dataFolder.mkdirs()) {
        throw new IOException("Couldn't create the data folder! ");
    }
    File actualFile = new File(dataFolder, resourceName);
    if (!actualFile.exists()) {
        if (!actualFile.createNewFile()) {
            throw new IOException("Could not create the configuration file \"" + resourceName + "\"!");
        }
        ByteStreams.copy(MatrixUHC.getInstance().getResource(resourceName), new FileOutputStream(actualFile));
    }
    YamlConfiguration defaultsConfig = YamlConfiguration
            .loadConfiguration(new InputStreamReader(MatrixUHC.getInstance().getResource(resourceName)));
    YamlConfiguration actualConfig = YamlConfiguration.loadConfiguration((actualFile));
    actualConfig.setDefaults(defaultsConfig);
    return new MatrixConfig(MatrixUHC.getInstance(), actualFile, actualConfig, resourceName);
}