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:io.github.bluemarlin.util.BluemarlineUtil.java

public static File loadOrCreateFile(File file) {
    if (!file.exists()) {
        try {//from   www . j a v a  2 s  .  co  m
            file.createNewFile();
        } catch (IOException e) {
            throw new BlackmarketRuntimeException(e);
        }
    }
    return file;
}

From source file:Main.java

public static OutputStream openFileStream(String directory, String filePath) {
    if (TextUtils.isEmpty(directory) || TextUtils.isEmpty(filePath)) {
        return null;
    }//  w  w w .  j av  a  2 s.c  o m

    File directoryFile = new File(directory);
    if (!directoryFile.exists()) {
        directoryFile.mkdirs();
    }

    File file = new File(directory + File.separator + filePath);
    try {
        if (!file.exists()) {
            file.createNewFile();
        }
        return new FileOutputStream(file, true);
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:Main.java

private static File createFileOrDir(String path, String fileName, boolean isDir) {
    if (path.charAt(path.length() - 1) != '/') {
        path += '/';
    }//  w  w  w  . ja v  a 2s.  co  m

    File file = new File(path + fileName);
    if (!isDir) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        file.mkdir();
    }
    return file;
}

From source file:Main.java

/**
 * Copy file from oldPath to newPath//  w ww  .j  a v  a  2  s. c om
 *
 * @param oldPath
 * @param newPath
 */
public static void copyFile(String oldPath, String newPath) {
    try {
        int byteread = 0;
        File oldfile = new File(oldPath);
        File newfile = new File(newPath);
        if (newfile.exists()) {
            newfile.delete();
        }
        newfile.createNewFile();
        if (oldfile.exists()) {
            FileInputStream inStream = new FileInputStream(oldPath);
            FileOutputStream outStream = new FileOutputStream(newPath);
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, byteread);
            }
            inStream.close();
            outStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.domnian.BotConfiguration.java

public static void load(File file) throws Exception {
    if (file.createNewFile()) {
        BotConfiguration.writeDefault(file);
    }/* ww  w. j a  v a  2  s.  c om*/
    FileInputStream fis = new FileInputStream(file);
    byte[] buffer = new byte[1024];
    fis.read(buffer);
    JSONObject configJson = new JSONObject(new String(buffer));
    connectJson = configJson.getJSONObject("connect");
    identJson = configJson.getJSONObject("ident");
    nickServJson = configJson.getJSONObject("auth");
    manage = configJson.getString("manage");
}

From source file:Main.java

public static void write(InputStream in, File file) {
    if (file.exists()) {
        file.delete();//from   w ww .ja  va  2s . c  o m
    }
    try {
        file.createNewFile();
        FileOutputStream out = new FileOutputStream(file);
        byte[] buffer = new byte[1024];
        while (in.read(buffer) > -1) {
            out.write(buffer);
        }
        out.flush();
        in.close();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean createFile(String name) {
    File outfile = new File(name);
    BufferedWriter writer = null;
    if (!outfile.isFile()) {
        try {/*  ww  w. j a  va  2 s  .c  om*/
            outfile.createNewFile();
            writer = new BufferedWriter(new FileWriter(outfile));
            writer.write(
                    "#_*_ coding: iso8859_1\n# Script API\n\nfrom com.android.python import AndroidDriver\n\n");
            writer.flush();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

public static void getStringFromXML(Node node, String dtdFilename, String outputFileName)
        throws TransformerException, IOException {
    File file = new File(outputFileName);
    if (!file.isFile())
        file.createNewFile();
    BufferedWriter out = new BufferedWriter(new FileWriter(file));

    String workingPath = System.setProperty("user.dir", file.getAbsoluteFile().getParent());
    try {// w w w .  j  a  v a2  s  . co m
        getStringFromXML(node, dtdFilename, out);
    } finally {
        System.setProperty("user.dir", workingPath);
    }

    out.flush();
    out.close();
}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {

    new File(destFile.getParent()).mkdirs();

    if (!destFile.exists()) {
        destFile.createNewFile();
    }//ww w  . j ava2s  .  c  o m

    FileChannel source = null;
    FileChannel destination = null;

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

From source file:Main.java

public static void appendLog(String text) {
    Log.d("LOGFILE", text);
    SimpleDateFormat sTime = new SimpleDateFormat("dd/MMM/yyyy - hh:mm:ss");
    String timeFormat = sTime.format(new Date());

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/Cura/Logs/");
    dir.mkdirs();/*w  w w .  j a  v a2  s  .c om*/
    File logFile = new File(dir, "Cura_Logs_DEBUG.txt");
    if (!logFile.exists()) {
        try {
            logFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try {
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
        if (text.compareTo("wipe") == 0)
            logFile.delete();
        else {
            buf.append("[" + timeFormat + "] - ");
            buf.append(text);
            buf.newLine();
            buf.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}