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

public static boolean createFile(File file, boolean recursion) throws IOException {
    boolean result = false;
    if (!file.exists()) {
        try {//from   w w w.j a  va2 s .  com
            result = file.createNewFile();
        } catch (IOException e) {
            if (!recursion) {
                throw e;
            }
            File parent = file.getParentFile();
            if (!parent.exists())
                parent.mkdirs();
            try {
                result = file.createNewFile();
            } catch (IOException e1) {
                throw e1;
            }
        }
    }
    return result;
}

From source file:Main.java

public static void saveToFile(String fileName, String str) throws IOException {
    File f = new File(fileName);
    if (!f.exists()) {
        if (f.getParent() != null) {
            new File(f.getParent()).mkdirs();
        }/*from w w  w  . j av a2  s .  com*/
        f.createNewFile();
    }
    FileWriter fw = new FileWriter(f);
    fw.write(str);
    fw.close();
}

From source file:Main.java

public static void saveImage(Bitmap bmp, String name) {
    if (bmp != null) {
        File appDir = new File(Environment.getExternalStorageDirectory(), name);
        if (!appDir.exists()) {
            try {
                appDir.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();//w  ww  . j av  a2  s .c o m
            }
        }
        try {
            FileOutputStream fos = new FileOutputStream(appDir);
            bmp.compress(Bitmap.CompressFormat.JPEG, 90, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {

    }
}

From source file:com.varaneckas.hawkscope.util.IOUtils.java

public static synchronized boolean copyFile(final String in, final String out) {
    try {//from   w  w w. j a  va 2 s  .  com
        File outFile = new File(out);
        if (!outFile.exists()) {
            if (!outFile.createNewFile()) {
                return false;
            }
        }
        return copyFile(ClassLoader.getSystemClassLoader().getResourceAsStream(in),
                new FileOutputStream(outFile));
    } catch (final Exception e) {
        log.debug("Failed copying file: " + in + " -> " + out, e);
        return false;
    }
}

From source file:Main.java

public static void saveBoardToExternal(Context context, String fileName, JSONObject jsonObject) {
    File file = new File(getDirectoryBoards(), fileName);
    if (file.exists()) {
        file.delete();//from  w w  w .j av  a  2  s .  c om
    }

    try {
        file.createNewFile();
        BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
        buf.append(jsonObject.toString());
        buf.close();
        addTomMediaScanner(context, file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean saveFile(String filePath, String str) throws IOException {
    boolean result = false;
    if (filePath != null && str != null) {
        Log.d(TAG, "filePath:" + filePath);
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();//w  w w  . j a  va  2s.  c  o  m
        }
        if (file.createNewFile()) {
            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
            fos.write(str.getBytes("gb18030"));
            fos.flush();
            fos.close();
            result = true;
        }
    }
    return result;
}

From source file:Main.java

private static boolean checkFsWritable() {
    String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
    File directory = new File(directoryName);
    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }//  w ww . java  2s.c  o m
    }
    File f = new File(directoryName, ".probe");
    try {
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;
    } catch (IOException ex) {
        return false;
    }
}

From source file:com.ms.commons.standalone.job.JobRunner.java

private static void stopJob() {
    String standaloneStopPath = System.getProperty(Cons.STANDALONE_STOP_FILE);
    File standaloneStopFile = new File(standaloneStopPath);
    if (!standaloneStopFile.exists()) {
        try {//from  ww  w .j  av a  2  s . com
            standaloneStopFile.createNewFile();
        } catch (IOException e) {
            throw new RuntimeException("create standalone stop file failed path: " + standaloneStopPath, e);
        }
    }
}

From source file:Main.java

/**
 * Creates a new XML file and writes the content of the specified object into the XML file. 
 * @param filepath            Path to the XML file.
 * @param content            Content as an object of the specified class.
 * @param typeParameterClass   Class of the object with the content.
 * @return                  File./*  w  w w. j  av  a 2s  . c  o  m*/
 */
public static <T> File write(String filepath, T content, Class<T> typeParameterClass) {
    File file = null;
    try {
        file = new File(filepath);
        if (!file.exists()) {
            file.createNewFile();
        }
        file = write(file, content, typeParameterClass);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:net.mindengine.dashserver.compiler.WidgetCompiler.java

private static Writer createWriter(File targetFile) throws IOException {
    try {//from   w ww. ja v a 2 s  .c om
        targetFile.createNewFile();
    } catch (IOException ex) {
        throw new IOException("Cannot create file: " + targetFile.getAbsolutePath(), ex);
    }
    return new FileWriter(targetFile);
}