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 writeBuffer2InternalStorage(String path, byte[] data) {
    File file = new File(_context.getFilesDir() + File.separator + path);

    try {//from  w  ww .j  a  v a2 s . c o m
        FileOutputStream outstream;

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

        outstream = new FileOutputStream(file);
        outstream.write(data);
        outstream.close();

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

From source file:Main.java

/**
 * Create a new file, if the file exists, delete and create again.
 *
 * @param targetFile file.//from   w  ww .j  a  va2  s .  c o m
 * @return True: success, or false: failure.
 */
public static boolean createNewFile(File targetFile) {
    if (targetFile.exists())
        delFileOrFolder(targetFile);
    try {
        return targetFile.createNewFile();
    } catch (IOException e) {
        return false;
    }
}

From source file:com.feilong.tools.net.ZClientTest.java

/**
 * Gets the files./*w  ww.j  ava2  s.co  m*/
 * 
 * @param ftp
 *            the ftp
 * @param localDir
 *            the local dir
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private static void testGetFiles(FTPClient ftp, File localDir) throws IOException {
    String[] names = ftp.listNames();
    for (String name : names) {
        File file = new File(localDir.getPath() + File.separator + name);
        if (!file.exists()) {
            file.createNewFile();
        }
        long pos = file.length();
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(pos);
        ftp.setRestartOffset(pos);
        InputStream is = ftp.retrieveFileStream(name);
        if (is == null) {
            log.info("no such file:" + name);
        } else {
            log.info("start getting file:" + name);
            int b;
            while ((b = is.read()) != -1) {
                raf.write(b);
            }
            is.close();
            if (ftp.completePendingCommand()) {
                log.info("done!");
            } else {
                log.info("can't get file:" + name);
            }
        }
        raf.close();
    }
}

From source file:Main.java

public static int checkFileStatus(String dirPath, String fileName, String suffix) {
    int fileStatus;
    String filePath = checkFilePath(dirPath, fileName, suffix);

    if (filePath == null) {
        return fileStatus = FILE_ERROR;
    }/*from   w  ww.  j  a  v a 2s.  c o m*/

    try {
        File dirFile = new File(dirPath);
        if (!dirFile.exists()) {
            dirFile.mkdirs();
        }
        File mFile = new File(filePath);
        if (!mFile.exists()) {
            fileStatus = FILE_CREATE;
            mFile.createNewFile();
        } else {
            long size = mFile.length();
            if (size > 0) {
                fileStatus = FILE_EXISTS;
            } else {

                fileStatus = FILE_EXISTS_NULL;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        return FILE_ERROR;
    }
    return fileStatus;

}

From source file:com.oozierunner.core.FileManager.java

public static BufferedWriter getFileWriter(String fileName) throws IOException {

    System.out.println("File to write in :-> " + fileName);

    File file = new File(fileName);

    // if file doesnt exists, then create it
    if (!file.exists()) {
        file.createNewFile();
    }/*from www  .j  a v  a2  s .  c  om*/

    FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

    return bufferedWriter;
}

From source file:Main.java

public static File setOutPutImage(String name) {
    File outputimage = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
    if (outputimage.exists()) {
        outputimage.delete();//  w w  w  .j  ava 2 s  .c  om
    }
    try {
        outputimage.createNewFile();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outputimage;
}

From source file:Main.java

public static boolean cacheDrawable(String packageName, int resId, BitmapDrawable drawable) {
    try {//from  ww  w .  j  a  va  2 s .  c o m
        File f = new File("/data/data/" + PACKAGE_NAME + "/cache/icons/", packageName + "_" + resId);
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        drawable.getBitmap().compress(CompressFormat.PNG, 0, bos);
        new FileOutputStream(f).write(bos.toByteArray());
        f.setReadable(true, false);
        f.setWritable(true, false);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:com.pseudosudostudios.jdd.utils.ScoreSaves.java

private static void writeFile(Context c, List<Entry> entries) {
    Collections.sort(entries);//from   ww w.  j  av  a  2 s  . c o m
    File file = new File(c.getFilesDir(), fileName);
    try {
        file.createNewFile();
        JSONArray array = new JSONArray();
        for (Entry e : entries)
            array.put(makeJSONObject(e));
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(array.toString());
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}

From source file:com.z2data.files.WriteOperations.java

/**
 * Write error URL in file/*from  w w w .j  av  a2  s . c  om*/
 * 
 * @param outputPath
 * @param URL
 * @param errorMessage
 */
public static void writeErrorData(String outputPath, String URL, String errorMessage) {
    File file = null;
    try {
        file = new File(outputPath);

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

        String errorFileName = "ERRORS_URLS_" + file.getName();
        File errorFile = null;
        if (file.getParent() == null || file.getParent().isEmpty()) {
            errorFile = new File(errorFileName);
        } else {
            errorFile = new File(file.getParent() + errorFileName);
        }

        if (!errorFile.exists()) {
            errorFile.createNewFile();
        }
        FileUtils.writeStringToFile(errorFile, URL + "\t" + errorMessage, true);
        FileUtils.writeStringToFile(errorFile, System.lineSeparator(), true);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:cz.incad.cdk.RepairVCProcess.java

private static File backup(String pid, Document relsExt) throws TransformerException, IOException {
    LOGGER.info("backup file for pid '" + pid + "'");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    XMLUtils.print(relsExt, bos);/*from  w ww  . ja v  a 2s .  co  m*/
    File backupFile = new File(pid);
    backupFile.createNewFile();
    IOUtils.copyStreams(new ByteArrayInputStream(bos.toByteArray()), new FileOutputStream(backupFile));
    return backupFile;
}