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

/**
 * Create file if !exist/*from   w ww  .j  av a  2  s .co  m*/
 * 
 * @param filePath
 *            The absolute file path we need to create
 * 
 * @throws IOException
 *             Input/Output exceptions
 */
public static void createFile(File filePath) {
    if (!filePath.exists()) {
        try {
            filePath.createNewFile();
            Log.d("RDWR", "createFile: " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
            Log.d("RDWR", "CreateFile: " + e.getMessage());
        }
    }
}

From source file:Main.java

public static void saveImageToPath(Bitmap bitmap, String imagepath) {
    File file = new File(imagepath);
    try {//from   w w  w .  j  av a2 s. co  m
        if (!file.exists()) {
            file.createNewFile();
        }
        bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream(file));// quality = 100
    } catch (Exception e) {
    }
}

From source file:Main.java

public static boolean putFileContent(File file, InputStream inputStream) throws IOException {
    if (!file.exists() && !file.createNewFile()) {
        return false;
    }//from  ww  w .java2s .  c o  m

    byte[] buffer = new byte[1024];
    FileOutputStream fileOutputStream = new FileOutputStream(file);

    for (int len; (len = inputStream.read(buffer)) != -1;) {
        fileOutputStream.write(buffer, 0, len);
    }

    inputStream.close();
    fileOutputStream.close();
    return true;
}

From source file:Main.java

public static synchronized void writeToFile(String rootPath, String filename, String data) {

    File file = new File(rootPath);
    if (!file.exists()) {
        file.mkdirs();/*from   w ww .  j  av  a  2 s .  c om*/

    }

    FileOutputStream fOut = null;
    try {

        File savedfile = new File(rootPath + filename);
        savedfile.createNewFile();//create file if not exists
        fOut = new FileOutputStream(savedfile, true); //append content to the end of file
        OutputStreamWriter outWriter = new OutputStreamWriter(fOut);

        outWriter.write(data);

        fOut.flush();
        outWriter.flush();

        fOut.close();
        outWriter.close();
    } catch (IOException e) {

        e.printStackTrace();
    }

}

From source file:Main.java

public static void copyFile(InputStream in, File dest) throws IOException {
    if (!dest.exists()) {
        dest.createNewFile();
    }/*from   w  w  w  .  java 2  s. com*/
    OutputStream out = null;
    try {
        out = new FileOutputStream(dest);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }
}

From source file:Main.java

public static boolean makeFile(String filePath) {
    File f = new File(filePath);
    if (!f.exists()) {
        try {/*ww w .  j a va2s .c  o m*/
            return f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    } else {
        return true;
    }
}

From source file:Main.java

public static boolean isFileValid(File f) {
    if (!f.exists()) {
        try {//  w ww . j av  a 2  s  . c om
            f.createNewFile();
        } catch (IOException e) {

            return false;
        }
        f.delete();
    }
    return true;
}

From source file:Main.java

public static void createNoMediaFile(String src) {
    File file = new File(src + "/.nomedia");
    if (!file.exists()) {
        try {//from w ww.j av a 2 s . c  o  m
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static void writeListToFile(String filepath, List<String> data) {
    try {// w w w.j  a  v  a  2s .  c  o m
        File file = new File(filepath);
        if (!file.exists()) {
            file.createNewFile();
        }

        FileWriter fileWriter = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
        for (String line : data) {
            bufferedWriter.write(line);
            bufferedWriter.newLine();
        }

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

From source file:Main.java

public static void saveMyBitmap(Bitmap mBitmap) {
    File f = new File(FileSavePath + "qrcode.png");
    try {// www .j av  a 2 s  .c  o  m
        f.createNewFile();
    } catch (IOException e) {
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
    } catch (Exception e) {
        e.printStackTrace();
    }
    mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        fOut.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}