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 void saveNamedFile(String patch, String data, String name) {

    try {// w  w w.j a va  2s  .c  o  m

        File direct = new File(patch);
        if (!direct.exists())
            direct.mkdirs();

        File f = new File(patch + "/" + name);
        if (!f.exists())
            f.createNewFile();

        FileOutputStream fos = new FileOutputStream(f);
        String s = data;
        fos.write(s.getBytes("UTF-8"));
        fos.close();

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }

}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        destFile.createNewFile();
    }/*from www .j a v  a 2s. co m*/

    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();
        }
    }
}

From source file:Main.java

public static ZipOutputStream createZipOutputStream(File file, int compressLevel) {
    try {/* ww  w  .java  2s  . c  om*/
        if (!file.exists()) {
            file.createNewFile();
        }
        final FileOutputStream fos = new FileOutputStream(file);
        final ZipOutputStream zos = new ZipOutputStream(fos);
        zos.setLevel(compressLevel);
        return zos;
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:Main.java

public static void writeUserInfo(String[] info, Context c) throws IOException {
    File f = c.getFileStreamPath(userInfoPath);
    if (!f.exists()) {
        f.createNewFile();
    }//from  w w  w.  j  a v  a2s.co m

    String output = "";
    for (int n = 0; n < info.length; n++) {
        output += info[n] + eol + separator + eol;
    }
    BufferedWriter out = null;
    out = new BufferedWriter(new OutputStreamWriter(c.openFileOutput(userInfoPath, Context.MODE_PRIVATE)));

    out.write(output);
    out.flush();
    out.close();
    userInfo = info;
}

From source file:Main.java

/** Create the EasyRPG's directories in path it's possible */
public static boolean createEasyRPGDirectories(String path) {
    //Main folder
    File dir = new File(path);
    dir.mkdir();//from  w  ww .ja v a 2 s  . c om

    //Games' folder
    File dirGames = new File(dir, "games/");
    dirGames.mkdir();

    //RTP's folders
    File dirRtp = new File(dir, "rtp/");
    dirRtp.mkdir();
    File dirRtp2000 = new File(dirRtp, "2000");
    dirRtp2000.mkdir();
    File dirRtp2003 = new File(dirRtp, "2003");
    dirRtp2003.mkdir();

    // The .nomedia file (to not let app scan games and RTP's folders)
    File nomediafile = new File(dir, ".nomedia");
    try {
        nomediafile.createNewFile();
    } catch (IOException e) {
        Log.e("Create File", "Error creating .nomedia file");
    }

    //TODO : Verify if all the folders exists
    return true;
}

From source file:Main.java

public static void createCSV() {
    File folder = new File(Environment.getExternalStorageDirectory() + "/adspammer");
    if (!folder.exists())
        folder.mkdir();/*from  www .j  a  va  2  s.co m*/

    String filename = folder.toString() + "/" + "log.csv";

    File csvFile = new File(filename);
    if (!csvFile.exists())
        try {
            csvFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

}

From source file:Main.java

public static void copyFile(File sourceFile, File destFile) throws IOException {
    if (!destFile.exists()) {
        if (!destFile.createNewFile()) {
            throw new IOException("Cannot create file: " + destFile.getCanonicalFile());
        }//w w w . ja v a 2s .  c  o  m
    }
    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        long count = 0;
        long size = source.size();
        while ((count += destination.transferFrom(source, count, size - count)) < size)
            ;
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

From source file:Main.java

public static void unZip(String path) {
    int count = -1;
    int index = -1;

    String savepath = "";

    savepath = path.substring(0, path.lastIndexOf("."));
    try {//from w w w  . j  a  va2s  .  c om
        BufferedOutputStream bos = null;
        ZipEntry entry = null;
        FileInputStream fis = new FileInputStream(path);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));

        while ((entry = zis.getNextEntry()) != null) {
            byte data[] = new byte[buffer];

            String temp = entry.getName();
            index = temp.lastIndexOf("/");
            if (index > -1)
                temp = temp.substring(index + 1);
            String tempDir = savepath + "/zip/";
            File dir = new File(tempDir);
            dir.mkdirs();
            temp = tempDir + temp;
            File f = new File(temp);
            f.createNewFile();

            FileOutputStream fos = new FileOutputStream(f);
            bos = new BufferedOutputStream(fos, buffer);

            while ((count = zis.read(data, 0, buffer)) != -1) {
                bos.write(data, 0, count);
            }

            bos.flush();
            bos.close();
        }

        zis.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static void saveStr(String path, String xml, boolean isAppend) {
    OutputStream out = null;/*from   w ww.  j a  v a2 s  . c o  m*/
    OutputStreamWriter outwriter = null;
    try {
        File file = new File(path);
        if (!file.exists()) {
            file.createNewFile();
        }
        out = new FileOutputStream(path, isAppend);
        outwriter = new OutputStreamWriter(out, "UTF-8");
        outwriter.write(xml);
        outwriter.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (outwriter != null) {
                outwriter.close();
            }
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static boolean saveDrawabletoFile(Context c, Drawable d, File file) {
    //create a file to write bitmap data
    try {//w ww .j  a v a2  s .c  o  m
        file.createNewFile();
        //Convert bitmap to byte array
        Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return false;
}