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

private static boolean checkFsWritable(String dir) {

    if (dir == null)
        return false;

    File directory = new File(dir);

    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }//from w w w.j ava 2  s  . c om
    }

    File f = new File(directory, ".keysharetestgzc");
    try {
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;

    } catch (Exception e) {
    }
    return false;

}

From source file:Main.java

public static File getFilePath(String filePath, String fileName) {
    File file = null;
    makeRootDirectory(filePath);/*from   w w w.  j  a  v a 2  s  . c o m*/
    try {
        file = new File(filePath + fileName);
        if (!file.exists()) {
            file.createNewFile();
        }

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return file;
}

From source file:com.pnf.jebauto.AutoUtil.java

/**
 * Create an Apache properties object out of a JEB2 configuration file.
 * /*from w  w w  .  j  a  va  2  s .co m*/
 * @param path path to a JEB2 configuration file, such as jeb-client.cfg or jeb-engines.cfg
 * @return
 */
public static PropertiesConfiguration createPropertiesConfiguration(String path) {
    File configfile = new File(path);

    if (!configfile.isFile()) {
        try {
            configfile.createNewFile();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    Parameters params = new Parameters();
    FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<>(
            PropertiesConfiguration.class).configure(params.properties().setFileName(path));
    builder.setAutoSave(true);

    try {
        return builder.getConfiguration();
    } catch (ConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:extractjavadoc.TraversalFiles.java

public static void fileList(File inputFile, int node, ArrayList<String> path, String folderPath,
        boolean ifGeneral, Map<String, Boolean> libraryTypeCondition) {
    node++;//from  w  w  w  .j  a v  a2  s.c  o m
    File[] files = inputFile.listFiles();
    if (!inputFile.exists()) {
        System.out.println("File doesn't exist!");
    } else if (inputFile.isDirectory()) {
        path.add(inputFile.getName());

        for (File f : files) {
            for (int i = 0; i < node - 1; i++) {
                System.out.print(" ");
            }
            System.out.print("|-" + f.getPath());

            String ext = FilenameUtils.getExtension(f.getName());
            if (ext.equals("html")) {
                try {
                    System.out.println(" => extracted");

                    //Get extracted file location and add it to output file name,
                    //in order to avoid files in different folder 
                    //have the same name.
                    String fileLocation = "";
                    for (String tmpPath : path) {
                        fileLocation += "-" + tmpPath;
                    }

                    String usefulJavadocFilePath = folderPath + "/" + "javadoc-" + f.getName() + fileLocation
                            + ".txt";
                    //                        String usefulJavadocFilePath = folderPath + "/" + "javadoc-" + f.getName() + ".txt";

                    //create output file for usefuljavadoc
                    File usefulJavadocFile = new File(usefulJavadocFilePath);
                    if (usefulJavadocFile.createNewFile()) {
                        System.out.println("Create successful: " + usefulJavadocFile.getName());
                    }

                    //extract useful javadoc
                    ExtractHTMLContent extractJavadoc = new ExtractHTMLContent(f, usefulJavadocFile, ifGeneral,
                            libraryTypeCondition);
                    extractJavadoc.extractHTMLContent();
                } catch (IOException ex) {
                    Logger.getLogger(TraversalFiles.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                //                   System.out.println(" isn't a java file or html file.");
                System.out.println(" isn't a html file.");
            }
            fileList(f, node, path, folderPath, ifGeneral, libraryTypeCondition);
        }
        path.remove(node - 1);
    }
}

From source file:TestRdfToJsonConversion.java

private static FileInputStream makeFile(String fnameJson) throws IOException {
    File f = new File(fnameJson);
    f.getParentFile().mkdirs();/* ww w .  j a  v a  2 s .com*/
    f.createNewFile();
    return new FileInputStream(f);
}

From source file:Main.java

public static void saveWeiboBitmap(Bitmap bitmap, String filename) {
    String status = Environment.getExternalStorageState();
    if (bitmap == null || !status.equals(Environment.MEDIA_MOUNTED)) {
        return;//from   w  w  w  . jav  a  2 s  . c  o m
    }
    File f = new File(filename);
    FileOutputStream fOut = null;
    boolean error = false;
    try {
        f.createNewFile();
    } catch (IOException e1) {
        e1.printStackTrace();
        error = true;
    }

    try {
        fOut = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        error = true;
    }

    try {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    } catch (Exception e) {
        e.printStackTrace();
        error = true;
    }

    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static boolean writeErrorLogToSDCard(String path, String errorLog) {
    if (!(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))) // check if SD card is mounted
        return false;

    File file;
    file = new File(path);
    if (!file.exists()) {
        try {//  w w w.j  a  v  a 2s .  c  o m
            file.getParentFile().mkdirs();
            file.createNewFile();
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
    }

    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, true)); //true means append to file
        writer.append(errorLog);
        writer.newLine();
        writer.append("-------------------------------"); // seperator
        writer.newLine();
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

public static boolean saveFile(String filePath, InputStream inputStream) throws IOException {
    boolean result = false;
    if (filePath != null && inputStream != null) {
        Log.d(TAG, "filePath:" + filePath);
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();/*from w ww  .  j ava2  s.co m*/
        }
        if (file.createNewFile()) {
            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
            byte[] buf = new byte[1024];
            int size = 0;
            while ((size = inputStream.read(buf, 0, 1024)) != -1) {
                fos.write(buf, 0, size);
            }

            fos.flush();
            fos.close();
            inputStream.close();
            result = true;
        }
    }
    return result;
}

From source file:de.flapdoodle.embedmongo.Files.java

public static File createTempFile(String tempFileName) throws IOException {
    File tempDir = new File(System.getProperty("java.io.tmpdir"));
    File tempFile = new File(tempDir, tempFileName);
    if (!tempFile.createNewFile())
        throw new IOException("Could not create Tempfile: " + tempFile);
    return tempFile;
}

From source file:it.restrung.rest.utils.IOUtils.java

/**
 * Serializes ans saves a Serializable object to a file
 *
 * @param object the source object//from w  w  w  . j  a  v  a  2 s .co m
 * @param file   the target file
 */
static public void saveSerializableObjectToDisk(Object object, File file) {
    try {
        file.delete();
        file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        GZIPOutputStream gzos = new GZIPOutputStream(fos);
        ObjectOutputStream out = new ObjectOutputStream(gzos);
        out.writeObject(object);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        Log.d(IOUtils.class.getName(), "Error, file not found for save serializable object to disk");
        throw new RuntimeException(e);
    } catch (IOException e) {
        Log.d(IOUtils.class.getName(), "Error on save serializable object");
        throw new RuntimeException(e);
    }
}