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.jdom.util.properties.PropertiesUtil.java

public static void writePropertiesFile(Properties properties, File outputFile) throws IllegalArgumentException {
    OutputStream os = null;/*from  www .  j a  va2s .  co m*/

    try {
        outputFile.delete();
        outputFile.getParentFile().mkdirs();
        outputFile.createNewFile();
        os = new FileOutputStream(outputFile);

        properties.store(os, "");
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:de.citec.sc.index.ProcessAnchorFile.java

public static void run(String filePath) {

    try {/*w  w w  .java 2s . c  o m*/
        File file = new File("new.ttl");

        // if file doesnt exists, then create it
        if (file.exists()) {
            file.delete();
            file.createNewFile();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedReader wpkg = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
        String line = "";

        PrintStream ps = new PrintStream("new.ttl", "UTF-8");

        while ((line = wpkg.readLine()) != null) {

            String[] data = line.split("\t");

            if (data.length == 3) {
                String label = data[0];

                label = StringEscapeUtils.unescapeJava(label);

                try {
                    label = URLDecoder.decode(label, "UTF-8");
                } catch (Exception e) {
                }

                String uri = data[1].replace("http://dbpedia.org/resource/", "");
                uri = StringEscapeUtils.unescapeJava(uri);

                try {
                    uri = URLDecoder.decode(uri, "UTF-8");
                } catch (Exception e) {
                }

                String f = data[2];

                label = label.toLowerCase();
                ps.println(label + "\t" + uri + "\t" + f);

            }

        }

        wpkg.close();
        ps.close();

        File oldFile = new File(filePath);
        oldFile.delete();
        oldFile.createNewFile();

        file.renameTo(oldFile);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.exalttech.trex.util.files.FileManager.java

/**
 * Export file//from  ww  w . jav a  2  s . c o  m
 *
 * @param windowTitle
 * @param fileName
 * @param fileContent
 * @param window
 * @param type
 * @throws IOException
 */
public static void exportFile(String windowTitle, String fileName, Object fileContent, Window window,
        FileType type) throws IOException {
    File savedFile = getSelectedFile(windowTitle, fileName, window, type,
            PreferencesManager.getInstance().getSaveLocation(), true);
    if (savedFile != null) {
        String filePath = savedFile.getAbsolutePath();
        String pathToSave = !filePath.contains(type.getExtension()) ? filePath + type.getExtension() : filePath;
        File fileToSave = new File(pathToSave);
        if (!fileToSave.exists()) {
            fileToSave.createNewFile();
        }
        if (fileContent instanceof String) {
            FileUtils.writeStringToFile(fileToSave, String.valueOf(fileContent));
        } else {
            FileUtils.copyFile((File) fileContent, savedFile);
        }
    }
}

From source file:adviewer.util.JSONIO.java

/**
 * Take in file and return one string of json data
 * // www .ja v a 2s  . c o m
 * @return
 * @throws IOException
 */
public static String readJSONFile(File inFile) {
    String readFile = "";

    try {
        File fileIn = inFile;

        //if the file is not there then create the file
        if (fileIn.createNewFile()) {
            System.out.println(fileIn + " was created ");
        }

        FileReader fr = new FileReader(fileIn);
        Scanner sc = new Scanner(fr);

        while (sc.hasNext()) {
            readFile += sc.nextLine().trim();
        }

        return readFile;

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

    // System.out.println( readFile  );
    return readFile;
}

From source file:fr.romainf.QRCode.java

/**
 * Writes data as a QRCode in an image.//from w  w w  .  jav a2 s  . c om
 *
 * @param data        String The data to encode in a QRCode
 * @param output      String The output filename
 * @param pixelColour Color The colour of pixels representing bits
 * @throws WriterException
 * @throws IOException
 */
private static void writeQRCode(String data, String output, Color pixelColour)
        throws WriterException, IOException {
    Map<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
    hints.put(EncodeHintType.MARGIN, DEFAULT_MARGIN);

    BitMatrix bitMatrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, DEFAULT_WIDTH, DEFAULT_HEIGHT,
            hints);

    File file = new File(output);
    if (file.isDirectory()) {
        file = new File(output + File.separator + DEFAULT_OUTPUT_FILE);
    }
    if (!file.createNewFile() && !file.canWrite()) {
        throw new IOException("Cannot write file " + file);
    }

    SVGGraphics2D graphics2D = renderSVG(bitMatrix, pixelColour);
    graphics2D.stream(new FileWriter(file), true);
}

From source file:com.splunk.shuttl.testutil.TUtilsFile.java

public static File createFileInParent(File parent, String fileName) {
    File child = new File(parent, fileName);
    try {//from www.  j a  v a 2 s .c om
        child.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
        TUtilsTestNG.failForException("Could not create file: " + child, e);
    }
    child.deleteOnExit();
    return child;
}

From source file:de.j4velin.mapsmeasure.Util.java

/**
 * Writes the given trace of points to the given file in CSV format,
 * separated by ";"//from   w w  w  .ja  v  a 2  s.c om
 * 
 * @param f
 *            the file to write to
 * @param trace
 *            the trace to write
 * @throws IOException
 */
static void saveToFile(final File f, final Stack<LatLng> trace) throws IOException {
    if (!f.exists())
        f.createNewFile();
    BufferedWriter out = new BufferedWriter(new FileWriter(f));
    LatLng current;
    for (int i = 0; i < trace.size(); i++) {
        current = trace.get(i);
        out.append(current.latitude + ";" + current.longitude + "\n");
    }
    out.close();
}

From source file:Main.java

/**
 * Create a file, If the file exists is not created.
 *
 * @param targetFile file./*from  w ww .  j  a v a2 s  . co m*/
 * @return True: success, or false: failure.
 */
public static boolean createFile(File targetFile) {
    if (targetFile.exists()) {
        if (targetFile.isFile())
            return true;
        delFileOrFolder(targetFile);
    }
    try {
        return targetFile.createNewFile();
    } catch (IOException e) {
        return false;
    }
}

From source file:ControllerParse.java

public static void parseProtocol(JSONObject protocolObject, String keyName, String folderPath,
        String packageName) throws IOException, JSONException {
    Iterator<String> keys = protocolObject.keys();

    if (null != keyName && keyName.length() > 0) {
        while (keys.hasNext()) {
            String key = keys.next();
            JSONObject item = protocolObject.optJSONObject(key);
            if (key.startsWith("POST")) {
                String interfaceName = key.replaceAll("POST /", "");
                interfaceName = interfaceName.replaceAll("/", "");
                JSONObject requestJson = item.optJSONObject("request");
                if (null != requestJson) {
                    ModelParse.parseProtocol(requestJson, interfaceName + "Request", folderPath, packageName);
                }//  w  w w .j a va 2 s .c  o  m

                JSONObject responseJson = item.optJSONObject("response");
                if (null != requestJson) {
                    ModelParse.parseProtocol(responseJson, interfaceName + "Response", folderPath, packageName);
                }
            }
        }
    }

    if (null != keyName && keyName.length() > 0) {
        keys = protocolObject.keys();

        File dirFolder = new File(folderPath + "/output");
        if (!dirFolder.exists()) {
            dirFolder.mkdirs();
        }

        File file = new File(folderPath + "/output/" + "ApiInterface" + ".java");

        if (file.exists()) {
            file.delete();
        }
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file, true);

        StringBuffer sb = new StringBuffer();
        sb.append("\n");
        sb.append("package " + packageName + ";\n");
        sb.append("public class ApiInterface \n{ ");
        sb.append("\n");

        while (keys.hasNext()) {
            String key = keys.next();
            JSONObject item = protocolObject.optJSONObject(key);
            if (key.startsWith("POST")) {
                String interfaceContent = key.replaceAll("POST ", "");

                String interfaceTemp = key.replaceAll("POST /", "");
                interfaceTemp = interfaceTemp.replaceAll("-", "_");
                String interfaceName = interfaceTemp.replaceAll("/", "_").toUpperCase();

                sb.append("     public static final String " + interfaceName + "  =" + "\"" + interfaceContent
                        + "\";\n");
            }
        }
        sb.append("\n}");
        out.write(sb.toString().getBytes("utf-8"));
        out.close();

    }
}

From source file:com.splicemachine.derby.impl.sql.actions.index.CsvUtil.java

public static void writeLines(String dirName, String fileName, Collection<String> content) throws Exception {
    File targetFile = new File(dirName, fileName);
    Files.createParentDirs(targetFile);
    if (targetFile.exists())
        targetFile.delete();//from ww w  .j  a  v  a  2 s . c  om
    targetFile.createNewFile();
    FileUtils.writeLines(targetFile, content);
}