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 File createTempFile(String prefix, String suffix, File dir, boolean isReCreat)
        throws IOException {
    int exceptionsCount = 0;
    while (true) {
        try {//w  w  w . ja  va2s .  c  o m
            File file = File.createTempFile(prefix, suffix, dir).getCanonicalFile();
            if (isReCreat) {
                file.delete();
                file.createNewFile();
            }
            return file;
        } catch (IOException ioex) { // fixes java.io.WinNTFileSystem.createFileExclusively access denied
            if (++exceptionsCount >= 50) {
                throw ioex;
            }
        }
    }
}

From source file:Main.java

public static boolean saveMyBitmap(File f, Bitmap mBitmap) throws IOException {
    boolean saveComplete = true;
    try {/*from   ww w  . j ava2  s .  co m*/
        f.createNewFile();
        FileOutputStream fOut = null;
        fOut = new FileOutputStream(f);
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();
        int finalWidth = 800;
        int finalHeight = (int) (finalWidth * 1.0 * (height * 1.0 / width * 1.0));
        double x = width * finalHeight;
        double y = height * finalWidth;

        if (x > y) {
            finalHeight = (int) (y / (double) width);
        } else if (x < y) {
            finalWidth = (int) (x / (double) height);
        }

        if (finalWidth > width && finalHeight > height) {
            finalWidth = width;
            finalHeight = height;
        }
        Matrix matrix = new Matrix();
        matrix.reset();
        float scaleWidth = ((float) finalWidth) / (float) width;
        float scaleHeight = ((float) finalHeight) / (float) height;
        matrix.postScale(scaleWidth, scaleHeight);
        mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, (int) width, (int) height, matrix, true);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
        fOut.flush();
        fOut.close();
        mBitmap.recycle();
        System.gc();
    } catch (FileNotFoundException e) {
        saveComplete = false;
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
        saveComplete = false;
    }
    return saveComplete;
}

From source file:Main.java

public static void initWidgetOneFile(Context context, String appId) {
    String root = null;//from  w w w.jav  a 2 s  .c  om
    appId += "/";
    if (sdCardIsWork()) {
        root = getSdCardRootPath();
    } else {
        root = context.getFilesDir().getAbsolutePath() + "/";
    }
    String[] fileDir = { root + F_APP_PATH + appId, root + F_WIDGET_PATH,
            root + F_APP_PATH + appId + F_APP_VIDEO, root + F_APP_PATH + appId + F_APP_PHOTO,
            root + F_APP_PATH + appId + F_APP_AUDIO, root + F_APP_PATH + appId + F_APP_MYSPACE };
    int size = fileDir.length;
    for (int i = 0; i < size; i++) {
        File file = new File(fileDir[i]);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
    String noMediaStr = root + F_BASE_WGT_PATH + ".nomedia";
    File noMedia = new File(noMediaStr);
    if (!noMedia.exists()) {
        try {
            noMedia.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean create(File file) throws IOException {
    if (file.exists()) {
        return true;
    }//from  ww  w  .j a  va2 s  .c o  m

    File parent = file.getParentFile();
    parent.mkdirs();
    return file.createNewFile();
}

From source file:Main.java

public static void createNomedia(Context context) {
    try {//  ww  w  . java2s .c  o  m
        File file = new File(context.getExternalFilesDir(null).getAbsolutePath() + NOMEDIA_FILE);
        //file.mkdirs(); ???
        if (!file.exists()) {
            file.createNewFile();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void byteArrayToFile(byte[] bytes, String filePath) throws Exception {
    InputStream in = new ByteArrayInputStream(bytes);
    File destFile = new File(filePath);
    if (!destFile.getParentFile().exists()) {
        destFile.getParentFile().mkdirs();
    }// w w  w  . j  a va  2 s. c o  m
    destFile.createNewFile();
    OutputStream out = new FileOutputStream(destFile);
    byte[] cache = new byte[CACHE_SIZE];
    int nRead = 0;
    while ((nRead = in.read(cache)) != -1) {
        out.write(cache, 0, nRead);
        out.flush();
    }
    out.close();
    in.close();
}

From source file:Main.java

public static boolean testNewTfFile() {
    File testFile = new File(T_FLASH_PATH, "testNewFile");
    boolean returnFlag = false;
    if (!testFile.exists()) {
        try {//from   ww w  .j a v a  2s  .c o  m
            if (testFile.createNewFile()) {
                returnFlag = true;
                testFile.delete();
            }
        } catch (IOException e) {
            returnFlag = false;
        }
    } else {
        testFile.delete();
        returnFlag = true;
    }
    return returnFlag;
}

From source file:Main.java

/**
 * Write file to sdcard//from   w  w w.ja v  a 2 s . c  om
 * @param dir
 * @param bt
 * @return
 */
public synchronized static File writeToSDCard(String dir, byte[] bt) {
    File file = null;
    try {
        file = new File(dir);
        if (file.exists()) {
            return file;
        }
        file.createNewFile();

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bt);
        fos.close();
        return file;
    } catch (Exception e) {
        if (file.exists()) {
            file.delete();
        }
        e.printStackTrace();
    }
    return null;
}

From source file:com.eatnumber1.util.io.FileUtils.java

public static void createNewFile(@NotNull File file) throws IOException {
    if (!file.createNewFile())
        throw new IOException("Unable to create file.");
}

From source file:Main.java

public static boolean saveBitmap2File(Bitmap bitmap, String filePath, String fileName) {
    File file = new File(filePath, fileName);
    FileOutputStream fileOutputStream = null;
    try {/* w w w.  j  a va2 s. c  o  m*/
        if (!file.exists()) {
            file.createNewFile();
        }
        fileOutputStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return true;
}