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:com.company.et.service.JsonService.java

/**
 *
 * @param json String what we write to file
 *///from w  w  w  . ja  va 2s  . c  o m
public static void writeToFile(String json) {

    try {
        File file = new File(getFilename());
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(json);
        bw.close();

        // @TODO: add LOG message
    } catch (IOException e) {
        e.printStackTrace();

        // @TODO: add LOG message
    }

}

From source file:files.FileUtils.java

public static boolean WriteFile(String pathFileText, String fileContent) {
    try {//from  www. ja  v  a  2s . c  o  m
        File file = new File(pathFileText);

        //if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        //Collecting Data
        if (!CommonUtils.IsNullOrEmpty(fileContent)) {
            //true = append file
            FileWriter fileWritter = new FileWriter(file.getPath(), false);
            try (BufferedWriter bufferWritter = new BufferedWriter(fileWritter)) {
                bufferWritter.write(fileContent);
            }
            return true;
        }
    } catch (IOException ex) {
        return false;
    }
    return false;
}

From source file:eu.swiec.bearballin.common.io.FileIO.java

@Deprecated
public static void writePageSource(String fileName, String pageSource) throws IOException {
    File pageFile = new File(fileName);
    if (pageFile.createNewFile()) {
        OutputStream outStrem = new FileOutputStream(pageFile);
        outStrem.write(pageSource.getBytes("UTF-16"));
        // outStrem.write(pageSource.getBytes(), 0, pageSource.length());
        outStrem.close();/*from   w  ww .  java  2s.  com*/
    }

}

From source file:files.FileUtils.java

public static boolean WriteDatFile(String pathFileText, String fileContent) {
    try {/*from   w  w w .ja  va2s. c  o m*/
        File file = new File(pathFileText);

        //if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        //Collecting Data
        if (!IsNullOrEmpty(fileContent)) {
            //true = append file
            FileWriter fileWritter = new FileWriter(file.getPath(), true);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(fileContent);
            bufferWritter.close();

            return true;
        }
    } catch (Exception ex) {
        return false;
    }
    return false;
}

From source file:com.beginner.core.utils.FileUpload.java

/**
 * ?upload//from  www  .j av  a 2 s  .c om
 * @param in          ?InputStream
 * @param dir         ??
 * @param realName      ??
 * @throws IOException   IO
 */
private static String copyFile(InputStream in, String dir, String realName) throws IOException {
    File file = new File(dir, realName);
    if (!file.exists()) {
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
    FileUtils.copyInputStreamToFile(in, file);
    return realName;
}

From source file:com.baidu.terminator.manager.common.file.FileUtils.java

public static void createFile(String fileLocation) {
    File file = new File(fileLocation);
    File parentFile = file.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();/*from   www .  j a  va  2  s  . c om*/
    }
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            LOGGER.error("create file :" + fileLocation + " fail!");
        }
    }
}

From source file:Main.java

public static boolean writeByte(@NonNull File file, @NonNull String content) {
    if (file.isDirectory()) {
        return false;
    }//from w  ww  . j ava 2s.co m
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
        }
    }
    OutputStream out = null;
    try {
        out = new FileOutputStream(file);
        byte[] b = content.getBytes();
        out.write(b);
        out.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (Exception e) {
        return false;
    } finally {
        CloseableClose(out);
    }
}

From source file:Main.java

public static Boolean createFile(String path) {
    File file = new File(path);
    File parantFile = file.getParentFile();
    if (null != parantFile && !parantFile.exists() && !parantFile.mkdirs()) {
        return false;
    }/*from   w  w w .  j  a  v  a  2  s .  c  om*/

    try {
        file.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:io.proleap.vb6.TestGenerator.java

public static void generateTreeFile(final File vb6InputFile, final File outputDirectory) throws IOException {
    final File outputFile = new File(outputDirectory + "/" + vb6InputFile.getName() + TREE_EXTENSION);

    final boolean createdNewFile = outputFile.createNewFile();

    if (createdNewFile) {
        LOG.info("Creating tree file {}.", outputFile);

        final InputStream inputStream = new FileInputStream(vb6InputFile);
        final VisualBasic6Lexer lexer = new VisualBasic6Lexer(new ANTLRInputStream(inputStream));
        final CommonTokenStream tokens = new CommonTokenStream(lexer);
        final VisualBasic6Parser parser = new VisualBasic6Parser(tokens);
        final StartRuleContext startRule = parser.startRule();
        final String inputFileTree = TreeUtils.toStringTree(startRule, parser);

        final PrintWriter pWriter = new PrintWriter(new FileWriter(outputFile));

        pWriter.write(inputFileTree);//from  w  ww.  j av  a  2 s.co  m
        pWriter.flush();
        pWriter.close();
    }
}

From source file:com.loy.MicroServiceConsole.java

@SuppressWarnings("rawtypes")
static void init() {

    ClassPathResource classPathResource = new ClassPathResource("application.yml");
    Yaml yaml = new Yaml();
    Map result = null;//  w w  w.j a  v  a 2s. c o m
    try {
        result = (Map) yaml.load(classPathResource.getInputStream());
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    String platformStr = result.get("platform").toString();
    String version = result.get("version").toString();
    String jvmOption = result.get("jvmOption").toString();
    @SuppressWarnings("unchecked")
    List<String> projects = (List<String>) result.get("projects");
    platform = Platform.valueOf(platformStr);

    final Runtime runtime = Runtime.getRuntime();
    File pidsForder = new File("./pids");
    if (!pidsForder.exists()) {
        pidsForder.mkdir();
    } else {
        File[] files = pidsForder.listFiles();
        if (files != null) {
            for (File f : files) {
                f.deleteOnExit();
                String pidStr = f.getName();
                try {
                    Long pid = new Long(pidStr);
                    if (Processes.isProcessRunning(platform, pid)) {
                        if (Platform.Windows == platform) {
                            Processes.tryKillProcess(null, platform, new NullProcessor(), pid);
                        } else {
                            Processes.killProcess(null, platform, new NullProcessor(), pid);
                        }

                    }
                } catch (Exception ex) {
                }
            }
        }
    }

    File currentForder = new File("");
    String root = currentForder.getAbsolutePath();
    root = root.replace(File.separator + "build" + File.separator + "libs", "");
    root = root.replace(File.separator + "e-example-ms-start", "");
    final String rootPath = root;
    new Thread(new Runnable() {
        @Override
        public void run() {
            int size = projects.size();
            int index = 1;
            for (String value : projects) {
                String path = value;
                String[] values = value.split("/");
                value = values[values.length - 1];
                value = rootPath + "/" + path + "/build/libs/" + value + "-" + version + ".jar";
                final String command = value;
                try {
                    Process process = runtime
                            .exec("java " + jvmOption + " -Dfile.encoding=UTF-8 -jar " + command);
                    Long pid = Processes.processId(process);
                    pids.add(pid);
                    File pidsFile = new File("./pids", pid.toString());
                    pidsFile.createNewFile();
                    new WriteLogThread(process.getInputStream()).start();

                } catch (IOException e) {
                    e.printStackTrace();
                }
                synchronized (lock) {
                    try {
                        if (index < size) {
                            lock.wait();
                        } else {
                            index++;
                        }

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }).start();
}