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:net.ftb.util.FTBFileUtils.java

public static void copyFile(File sourceFile, File destinationFile, boolean overwrite) throws IOException {
    if (sourceFile.exists()) {
        if (!destinationFile.exists()) {
            destinationFile.getParentFile().mkdirs();
            destinationFile.createNewFile();
        } else if (!overwrite) {
            return;
        }/*from   www  . j a  va 2s .  c om*/
        FileChannel sourceStream = null, destinationStream = null;
        try {
            sourceStream = new FileInputStream(sourceFile).getChannel();
            destinationStream = new FileOutputStream(destinationFile).getChannel();
            destinationStream.transferFrom(sourceStream, 0, sourceStream.size());
        } finally {
            if (sourceStream != null) {
                sourceStream.close();
            }
            if (destinationStream != null) {
                destinationStream.close();
            }
        }
    }
}

From source file:lyonlancer5.karasu.util.ModFileUtils.java

static void download(String url, File output) throws IOException {
    URL url1 = new URL(url);
    ReadableByteChannel rbc = Channels.newChannel(url1.openStream());
    FileOutputStream fos = new FileOutputStream(output);

    if (!output.exists()) {
        output.getParentFile().mkdirs();
        output.createNewFile();
    }//www .  ja va  2s. c o m

    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
    fos.close();
}

From source file:gr.abiss.calipso.util.AttachmentUtils.java

/**
 * @param file// ww w . j  a v a 2s.c  o  m
 * @throws IOException
 */
private static void ensureFileExists(File file) throws IOException {
    new File(file.getParent()).mkdirs();
    file.createNewFile();
}

From source file:atg.tools.dynunit.util.ComponentUtil.java

private static void recreateFile(@Nullable final File file) throws IOException {
    logger.entry(file);/*w w  w . j  a  v a2  s  . c  om*/
    if (file != null) {
        if (file.exists()) {
            FileUtils.forceDelete(file);
        }
        if (!file.createNewFile()) {
            throw logger.throwing(new IOException("File already exists but can't be deleted."));
        }
    }
    logger.exit();
}

From source file:com.hmiard.blackwater.projects.Builder.java

/**
 * Building a new project.// w w  w  .  j  ava2  s  . co m
 *
 * @param projectName String
 * @param serverNames ArrayList
 * @param location String
 */
public static void buildNewProject(String projectName, ArrayList<String> serverNames, String location,
        ConsoleEmulator consoleListener) {

    try {
        consoleListener.clean();
        consoleListener.push("Project creation started...\n\n");
        projectName = projectName.substring(0, 1).toUpperCase() + projectName.substring(1);
        String projectUrl = location + "\\" + projectName;

        buildFolder(projectUrl);
        buildFolder(projectUrl + "\\bin");
        buildFolder(projectUrl + "\\src");
        buildFolder(projectUrl + "\\.blackwater");

        ArrayList<String> realServerNames = new ArrayList<>();
        for (String name : serverNames)
            realServerNames.add(name.replace("!", ""));

        // Creating the configuration
        Properties configuration = new Properties();
        File tokens = new File(projectUrl + "\\.blackwater\\tokens.properties");
        configuration.setProperty("PATH", projectUrl);
        configuration.setProperty("NAME", projectName);
        configuration.setProperty("SERVERS", Parser.parseArrayToStringList(",", realServerNames));
        configuration.setProperty("LAST_UPDATE", new Date().toString());
        configuration.store(new FileOutputStream(tokens), "Blackwater build version : " + Start.VERSION);
        consoleListener.push("Tokens generated.\n");

        // Creating composer.json...
        JSONObject composerJSON = new JSONObject();
        JSONObject autoload = new JSONObject();
        JSONObject psr0 = new JSONObject();
        JSONObject require = new JSONObject();
        autoload.put("psr-0", psr0);
        composerJSON.put("autoload", autoload);
        composerJSON.put("require", require);

        require.put("blackwater/blackwaterp", Start.blackwaterpVersion);
        File composer = new File(projectUrl + "\\composer.json");
        if (!composer.createNewFile()) {
            consoleListener.push("\nWeird composer stuff happened... Aborting.\n");
            return;
        }

        BufferedWriter cw = new BufferedWriter(new FileWriter(composer.getAbsoluteFile()));
        String content = composerJSON.toString(4).replaceAll("\\\\", "");
        cw.write(content);
        cw.close();
        consoleListener.push("File created : composer.json\n");

        // Creating the servers...
        consoleListener.push("Server creation started...\n");
        for (String name : serverNames)
            if (name.charAt(0) == '!')
                appendServer(projectUrl, name.replace("!", ""), consoleListener, false);
            else
                appendServer(projectUrl, name, consoleListener, true);

        // Copying composer.phar
        consoleListener.push("Installing local composer wrapper...\n");
        copyFile(new File("resources/packages/composer.phar"),
                new File(composer.getParent() + "\\bin\\composer.phar"));

        // Building...
        consoleListener.push("Building dependencies...\n");
        new Thread(new ChildProcess("php bin/composer.phar install", composer.getParentFile(), consoleListener,
                () -> {
                    NewProjectAppScreenPresenter presenter = (NewProjectAppScreenPresenter) consoleListener.app;
                    presenter.projectCreatedCallback(projectUrl, consoleListener);
                })).start();

    } catch (JSONException | IOException e) {
        e.printStackTrace();
    }

}

From source file:com.smart.common.officeFile.CSVUtils.java

/**
 *
 * @param exportData ?//www.j a  v  a2 s.  c om
 * @param rowMapper ??
 * @param outPutPath 
 * @param filename ??
 * @return
 */
public static File createCSVFile(List exportData, LinkedHashMap rowMapper, String outPutPath, String filename) {

    File csvFile = null;
    BufferedWriter csvFileOutputStream = null;
    try {
        csvFile = new File(outPutPath + filename + ".csv");
        // csvFile.getParentFile().mkdir();
        File parent = csvFile.getParentFile();
        if (parent != null && !parent.exists()) {
            parent.mkdirs();
        }
        csvFile.createNewFile();

        // GB2312?","
        csvFileOutputStream = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"), 1024);
        // 
        for (Iterator propertyIterator = rowMapper.entrySet().iterator(); propertyIterator.hasNext();) {
            java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
            csvFileOutputStream.write("\"" + propertyEntry.getValue().toString() + "\"");
            if (propertyIterator.hasNext()) {
                csvFileOutputStream.write(",");
            }
        }
        csvFileOutputStream.newLine();

        // 
        for (Iterator iterator = exportData.iterator(); iterator.hasNext();) {
            LinkedHashMap row = (LinkedHashMap) iterator.next();
            System.out.println(row);

            for (Iterator propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext();) {
                java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
                // System.out.println(BeanUtils.getProperty(row, propertyEntry.getKey().toString()));
                csvFileOutputStream.write("\"" + propertyEntry.getValue().toString() + "\"");
                if (propertyIterator.hasNext()) {
                    csvFileOutputStream.write(",");
                }
            }

            //                for (Iterator propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext();) {
            //                    java.util.Map.Entry propertyEntry = (java.util.Map.Entry) propertyIterator.next();
            //                    System.out.println(BeanUtils.getProperty(row, propertyEntry.getKey().toString()));
            //                    csvFileOutputStream.write("\""
            //                            + BeanUtils.getProperty(row, propertyEntry.getKey().toString()) + "\"");
            //                    if (propertyIterator.hasNext()) {
            //                        csvFileOutputStream.write(",");
            //                    }
            //                }
            if (iterator.hasNext()) {
                csvFileOutputStream.newLine();
            }
        }
        csvFileOutputStream.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            csvFileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return csvFile;
}

From source file:com.bhbsoft.videoconference.record.convert.util.FlvFileHelper.java

public static File getNewFile(File dir, String name, String ext) throws IOException {
    File f = new File(dir, name + ext);
    int recursiveNumber = 0;
    while (f.exists()) {
        f = new File(dir, name + "_" + (recursiveNumber++) + ext);
    }//from   w  ww.java  2s.co  m
    f.createNewFile();
    return f;
}

From source file:com.bc.util.io.FileUtils.java

synchronized public static File createFileInTempDir(String tempDirName, String fileName) throws IOException {
    final File tempDir = new File(tempDirName);
    if (!tempDir.isDirectory()) {
        throw new IOException("The temp directory '" + tempDirName + "' does not exist");
    }/*  ww  w .j a v  a2 s. c o m*/

    final String name;
    final String extension;
    if (fileName.contains(".")) {
        final int i = fileName.lastIndexOf(".");
        name = fileName.substring(0, i);
        extension = fileName.substring(i);
    } else {
        name = fileName;
        extension = "";
    }

    File tempFile;
    int number = 0;
    do {
        tempFile = new File(tempDirName, name + (number > 0 ? number : "") + extension);
        number++;
    } while (tempFile.exists());
    tempFile.createNewFile();

    return tempFile;
}

From source file:net.firejack.platform.core.utils.MiscUtils.java

/**
 * @param properties//from ww  w .j  a  v a  2 s  .  c om
 * @param append
 * @throws java.io.IOException
 */
public static void setProperties(File properties, Map<String, String> append) throws IOException {
    Properties props = new Properties();
    if (properties.exists()) {
        FileReader reader = new FileReader(properties);
        props.load(reader);
        reader.close();
    } else {
        FileUtils.forceMkdir(properties.getParentFile());
    }

    if (properties.exists() || properties.createNewFile()) {
        props.putAll(append);
        FileWriter writer = new FileWriter(properties);
        props.store(writer, null);
        writer.flush();
        writer.close();
    }
}

From source file:dynamicrefactoring.util.io.FileManager.java

/**
 * Intenta crear un nuevo fichero en una ruta determinada. Si ya existe un
 * fichero con el mismo nombre en dicha ruta, simplemente no hace nada.
 * /*from  w  w  w.  j a v a  2s .c o m*/
 * @param path la ruta del nuevo fichero que se quiere crear.
 */
public static void createFile(String path) {
    try {
        File file = new File(path);

        file.createNewFile();
    } catch (IOException ioe) {
        ;
    }
}