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 storePropertyInstance(String filePath, String fileName, Properties p, String comment) {
    try {/*w  w w .ja v  a  2s  .  c  om*/
        File d = new File(filePath);
        if (!d.exists()) {
            d.mkdirs();
        }
        File f = new File(d, fileName);
        if (!f.exists()) {
            f.createNewFile();
        }
        OutputStream os = new FileOutputStream(f);
        p.store(os, comment);
        os.close();
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static boolean saveToSDCard(Bitmap bitmap) {
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return false;
    }//from w w w. j  a v a 2  s  .  c  o m
    FileOutputStream fileOutputStream = null;
    File file = new File("/sdcard/myName/Download/");
    if (!file.exists()) {
        file.mkdirs();
    }
    String fileName = UUID.randomUUID().toString() + ".jpg";
    String filePath = "/sdcard/myName/Download/" + fileName;
    File f = new File(filePath);
    if (!f.exists()) {
        try {
            f.createNewFile();
            fileOutputStream = new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
        } catch (IOException e) {
            return false;
        } finally {
            try {
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                return false;
            }
        }
    }
    return true;
}

From source file:Main.java

public static final void SaveObject(final String path, final Object saveObject) {
    FileOutputStream fileOutputStream = null;
    ObjectOutputStream objectOutputStream = null;
    File file = new File(path);
    try {//w w w.ja  v a  2  s.  com
        if (!file.exists()) {
            file.createNewFile();
        }
        fileOutputStream = new FileOutputStream(file);
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(saveObject);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (objectOutputStream != null) {
                objectOutputStream.close();
            }
            if (fileOutputStream != null) {
                fileOutputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean convertBytes2Bitmap(String imageName, byte[] byt) {
    if (byt.length == 0)
        return false;
    boolean success = true;
    Bitmap bmp = BitmapFactory.decodeByteArray(byt, 0, byt.length);
    File file = new File("/sdcard/" + imageName + ".png");

    try {/*from w  ww. j  ava2 s.  c  o  m*/
        file.createNewFile();
    } catch (IOException e) {
        success = false;
    }

    FileOutputStream out = null;

    try {
        out = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        success = false;
    }

    bmp.compress(Bitmap.CompressFormat.PNG, 100, out);

    try {
        out.flush();
    } catch (IOException e) {
        success = false;
    }

    try {
        out.close();
    } catch (IOException e) {
    }

    return success;
}

From source file:Main.java

public static String saveBitmapToFile(File dir, String name, Bitmap bitmap, String picKind) {
    File f = new File(dir, name + picKind);
    try {/*from  www .  j av a 2 s  . co  m*/
        f.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    CompressFormat format = CompressFormat.JPEG;
    if (picKind != null && picKind.equalsIgnoreCase(".png")) {
        format = CompressFormat.PNG;
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
        bitmap.compress(format, 100, fOut);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        if (fOut != null) {
            fOut.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if (fOut != null) {
            fOut.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    bitmap.recycle();
    bitmap = null;
    return f.getAbsolutePath();
}

From source file:Main.java

public static void WriteTxtFile(Context context, String strcontent) {

    String strContent = strcontent + "\n";
    try {//from   ww  w.  j  av a2  s. c om
        File file = new File(context.getCacheDir() + File.separator + "wanjia.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(file.length());
        raf.write(strContent.getBytes());
        raf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:fi.solita.phantomrunner.util.FileUtils.java

/**
 * Extracts the given resource to user's temporary directory (by creating a unique directory underneath
 * it). Will delete that file and created directories on system exit if deleteOnExit is true.
 * //from   w  w  w .  j av  a 2  s .  c o m
 * @param resource Resource to be extracted
 * @param subPath Slash (character '/') separated path of the sub-directories which should be created
 * @param deleteOnExit If the resource and created sub-directories should be deleted
 * 
 * @return File handle to the created file in the temporary directory
 */
public static File extractResourceToTempDirectory(Resource resource, String subPath, boolean deleteOnExit)
        throws IOException {
    final File tempDir = Files.createTempDir();
    if (deleteOnExit)
        tempDir.deleteOnExit();

    File lastDir = tempDir;
    for (String subDir : subPath.split("/")) {
        // if the subPath starts or ends with '/' we'll get empty strings too
        if (StringUtils.hasLength(subDir)) {
            lastDir = new File(lastDir, subDir);
            lastDir.mkdir();
            if (deleteOnExit)
                lastDir.deleteOnExit();
        }
    }

    final File resFile = new File(lastDir, resource.getFilename());
    resFile.createNewFile();
    if (deleteOnExit)
        resFile.deleteOnExit();

    IOUtil.copy(resource.getInputStream(), new FileWriter(resFile), "UTF-8");

    return resFile;
}

From source file:Main.java

/**
 * Adds a special, hidden file to the cache directory to prevent images from being indexed by Android Gallery
 * @param storageDir/*from  w w w.  j  a v  a 2s . com*/
 */
public static void setUpResourcesHiddenFromAndroidGallery(File storageDir) throws IOException {
    File hideGallery = new File(storageDir + "/.nomedia");
    if (hideGallery.exists()) {
        return;
    }
    hideGallery.createNewFile();
}

From source file:Main.java

public static void writeLifeLogCountInFile(int walking, int running, int vehicle, int bicycle, String filename)
        throws IOException {
    Date date = new Date();
    BufferedWriter writer = null;
    File dir = new File(Environment.getExternalStorageDirectory() + "/LifeLog");
    Log.d("Directory PATH", dir.getAbsolutePath());
    boolean flag = dir.mkdir();
    Log.d("Directory created?", "" + flag);
    File file = new File(dir.getAbsolutePath(), filename);
    if (file.exists() == false) {
        file.createNewFile();
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.write("Date,Walking,Running,Bicycle,Vehicle");
        writer.newLine();//from  www  . j  ava2  s . co  m
        writer.write(date.toString() + "," + walking + "," + running + "," + bicycle + "," + vehicle);
        writer.newLine();
    } else {
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.write(date.toString() + "," + walking + "," + running + "," + bicycle + "," + vehicle);
        writer.newLine();
        Log.d("Appended", "True");
    }
    writer.flush();
    writer.close();

}

From source file:Main.java

/**
 * Copy a file.//  ww w.j a v a 2s.  co  m
 * 
 * @param sourceFile
 *            The source
 * @param destDir
 *            The destination directory
 * @throws IOException
 *             Everything fails sometimes
 */
protected static void CopyFileToDir(File sourceFile, File destDir) throws IOException {

    File destFile = new File(destDir, sourceFile.getName());

    if (!destFile.exists()) {
        destFile.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

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