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.thoughtworks.go.helpers.FileSystemUtils.java

public static File createFile(String filename) throws IOException {
    File file = new File(ROOT + File.separator + filename);
    file.createNewFile();
    file.deleteOnExit();//from   w  ww.j  a va  2 s.c  o  m
    return file;
}

From source file:Main.java

private static boolean CopyFile(Context context, String pathName) {

    boolean isCopyCompleted = false;

    InputStream inputStream = null;
    OutputStream outputStream = null;

    try {/*from ww w .  ja v  a2 s  .c  o  m*/
        inputStream = context.getResources().getAssets().open(pathName);
        File outFile = new File(context.getCacheDir(), pathName);
        if (!outFile.exists()) {
            outFile.createNewFile();
        }

        outputStream = new FileOutputStream(outFile);

        byte[] buffer = new byte[4096];
        int bytesRead;
        // read from is to buffer
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
            Log.v("", "data..." + bytesRead);
        }

        isCopyCompleted = true;
        inputStream.close();
        // flush OutputStream to write any buffered data to file
        outputStream.flush();
        outputStream.close();

    } catch (IOException e) {
        isCopyCompleted = false;
        e.printStackTrace();
    }
    return isCopyCompleted;

}

From source file:Main.java

public static void printFile(InputStream is, String path) {
    FileOutputStream fos = null;//from  w  ww. j a v a2s  .  co  m
    byte[] temp = null;
    try {
        if (isPrint) {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            temp = new byte[1024];
            int i = 0;
            while ((i = is.read(temp)) > -1) {
                if (i < temp.length) {
                    byte[] b = new byte[i];
                    System.arraycopy(temp, 0, b, 0, b.length);
                    fos.write(b);
                } else {
                    fos.write(temp);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            is = null;
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            fos = null;
        }
        temp = null;
    }
}

From source file:Main.java

public static void logGlobalException(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {/* w w w  .j a va 2  s  . com*/
        File file = new File(dir + "/error_logs.log");
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file, true);

        for (String log : logs) {
            fos.write((log + "\n\n").getBytes());
        }
        fos.close();
    } catch (IOException ioe) {
        Log.e("AndroidUtils", "An exception has occured in logGlobalException!");
        ioe.printStackTrace();
    }
}

From source file:Main.java

public static void write(String in, File file, boolean append) {
    if (file.exists()) {
        file.delete();//from  ww  w . j  a  v a 2 s. c  o m
    }
    try {
        file.createNewFile();
        FileWriter fw = new FileWriter(file, append);
        fw.write(in);
        fw.flush();
        fw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String getOutputAudioPath(Context context, String mediaName) {
    File mediaStorageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_RINGTONES),
            AudioFolderName);/*from   ww w .jav a 2 s.  c  om*/

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    File noMediaFile = new File(mediaStorageDir, ".nomedia");
    if (!noMediaFile.exists()) {
        try {
            noMediaFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return mediaStorageDir.getPath() + File.separator + mediaName;
}

From source file:Main.java

public static void save(String path, Bitmap bitmap) {

    String name = path.substring(path.lastIndexOf("/"));
    File file = new File(SAVE_PATH + name);
    try {//from   w  w w  . j  a v  a  2  s .  c  o m
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        FileOutputStream out = new FileOutputStream(file);
        if (bitmap != null) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        }
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/** 
 * Write the <code>Document</code> in memory out to a standard XML file.
 * @param  doc  the <code>Document</code> object for the XML file to list
 * @param  strFilePath  the XML file to write to
 * @throws custom custom exception if file is not writeable
 * @throws ioe IOException if an error occurs on file creation
 * @throws fnfe FileNotFoundException if PrintWriter constructor fails
 *//*w w  w.java2s . com*/
public static void WriteXMLFile2(Document doc, String strFilePath) throws Exception {
    // open file for writing
    File file = new File(strFilePath);

    // ensure that file is writeable
    try {
        file.createNewFile();
        if (!file.canWrite()) {
            throw new Exception("FileNotWriteable");
        }
    } catch (IOException ioe) {
        throw ioe;
    }

    // create a PrintWriter to write the XML file to
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(file);
    } catch (FileNotFoundException fnfe) {
        throw fnfe;
    }

    // write out the serialized form
    pw.print(getXMLListing(doc));
    pw.close();
}

From source file:Main.java

public static void createDipPath(String file) {
    String parentFile = file.substring(0, file.lastIndexOf("/"));
    File file1 = new File(file);
    File parent = new File(parentFile);

    if (!file1.exists()) {
        parent.mkdirs();//from ww w.j a va  2 s.  com
        try {
            file1.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static FileOutputStream getFileOutputStream(String filePath) throws IOException {
    FileOutputStream out;//from  w w w  .  j ava2 s  .co  m
    File file = new File(filePath);
    if (file.getParentFile().exists()) {
        file.getParentFile().delete();
    }
    file.getParentFile().mkdirs();
    file.createNewFile();
    out = new FileOutputStream(file);
    return out;
}