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) {
    if (file.isDirectory()) {
        file.mkdirs();/*from w w w  . ja  va  2s.c om*/
    } else if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs();
    }
    try {
        file.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }

    return true;
}

From source file:ec.edu.chyc.manejopersonal.managebean.util.BeansUtils.java

/***
 * Dado el archivo de fileUpload de primefaces (en el evento fileUpload), sube el archivo a un destino
 * @param origen Variable tipo UploadedFile obtenida del evento fileUpload del componente de primefaces, ah se obtendr el archivo
 * @param destino Archivo donde se guardar el archivo
 * @throws IOException Excepcin ocurrida al crear, leer o escribir el archivo
 *//*w ww.ja va2s. c o  m*/
public static void subirArchivoPrimefaces(UploadedFile origen, File destino) throws IOException {
    destino.createNewFile();

    FileOutputStream fileOutputStream = null;
    InputStream inputStream = null;
    try {
        fileOutputStream = new FileOutputStream(destino);
        byte[] buffer = new byte[ServerUtils.BUFFER_SIZE];
        int bulk;
        inputStream = origen.getInputstream();
        while (true) {
            bulk = inputStream.read(buffer);
            if (bulk < 0) {
                break;
            }
            fileOutputStream.write(buffer, 0, bulk);
            fileOutputStream.flush();
        }
        fileOutputStream.close();
        inputStream.close();
    } finally {
        if (fileOutputStream != null) {
            fileOutputStream.close();
        }
        if (inputStream != null) {
            inputStream.close();
        }
    }
}

From source file:Main.java

private static boolean checkFsWritable() {
    // Create a temporary file to see whether a volume is really writeable.
    // It's important not to put it in the root directory which may have a
    // limit on the number of files.
    String directoryName = Environment.getExternalStorageDirectory().toString() + "/DCIM";
    File directory = new File(directoryName);
    if (!directory.isDirectory()) {
        if (!directory.mkdirs()) {
            return false;
        }// w w w.  ja v  a 2s . c o  m
    }
    File f = new File(directoryName, ".probe");
    try {
        // Remove stale file if any
        if (f.exists()) {
            f.delete();
        }
        if (!f.createNewFile()) {
            return false;
        }
        f.delete();
        return true;
    } catch (IOException ex) {
        return false;
    }
}

From source file:Main.java

public static File getFileFromBytes(byte[] b, String outputFile) {
    BufferedOutputStream stream = null;
    File file = null;
    FileOutputStream fstream = null;
    try {/*from   w  w w.j a va  2 s . com*/
        file = new File(outputFile);
        if (!file.exists()) {
            file.createNewFile();
        }
        fstream = new FileOutputStream(file);
        stream = new BufferedOutputStream(fstream);
        stream.write(b);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (stream != null) {
        try {
            stream.close();
            fstream.close();

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

    }

    System.gc();

    return file;
}

From source file:Main.java

/**
 * /*from ww  w. j a v a  2 s.c  o  m*/
 * copy file
 * 
 * @param src
 *            source file
 * @param dest
 *            target file
 * @throws IOException
 */
public static void copyFile(File src, File dest) throws IOException {
    FileChannel inChannel = null;
    FileChannel outChannel = null;
    try {
        if (!dest.exists()) {
            dest.createNewFile();
        }
        inChannel = new FileInputStream(src).getChannel();
        outChannel = new FileOutputStream(dest).getChannel();
        inChannel.transferTo(0, inChannel.size(), outChannel);
    } finally {
        if (inChannel != null) {
            inChannel.close();
        }
        if (outChannel != null) {
            outChannel.close();
        }
    }
}

From source file:amazonechoapi.AmazonEchoApi.java

private static void addItemId(String itemId) throws IOException {
    File file = new File("Items.txt");
    try {/*from   w  w w  .j  av a  2  s  . c  o  m*/
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fileWritter = new FileWriter(file.getName(), true);
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        bufferWritter.write(itemId + "\n");
        bufferWritter.close();
    } catch (Exception e) {
    }
}

From source file:Main.java

public static void logScanReport(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {// w  ww . j  a v  a 2 s.co m
        File file = new File(dir + "/last_scan.log");
        boolean append = false;
        // if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        } else {
            /*
            if(file.length() >= 2097152){
               // set log file max size to 2mb
               append = false;
            }
             */
        }
        FileOutputStream fos = new FileOutputStream(file, append);

        for (String log : logs) {
            fos.write((log + "\n\n").getBytes());
        }
        fos.close();
    } catch (IOException ioe) {
        Log.e("AndroidUtils", "An exception has occured in logScanReport!");
        ioe.printStackTrace();
    }
}

From source file:Main.java

public static boolean createFile(String filePath) {
    try {/* ww  w  .ja  v  a  2s .  c  om*/
        File file = new File(filePath);
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

            return file.createNewFile();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:Main.java

/**
 * Saves a file from the given URL to the given filename and returns the file
 * @param link URL to file//  w w w  .j  av  a 2 s .c  om
 * @param fileName Name to save the file
 * @return The file
 * @throws IOException Thrown if any IOException occurs
 */
public static File saveFileFromNet(URL link, String fileName) throws IOException {
    InputStream in = new BufferedInputStream(link.openStream());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    int n = 0;
    while (-1 != (n = in.read(buf))) {
        out.write(buf, 0, n);
    }
    out.close();
    in.close();
    byte[] response = out.toByteArray();

    File file = new File(fileName);
    if (!file.exists()) {
        if (file.getParentFile() != null) {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
    }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(response);
    fos.close();

    return new File(fileName);
}

From source file:Main.java

public static boolean createFile(String filename) {
    final File file = new File(filename);
    final File pf = file.getParentFile();
    if (!pf.exists()) {
        pf.mkdirs();/*from w w w .j  a  va  2 s . com*/
    }
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    return true;
}