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:com.lostad.app.base.util.DownloadUtil.java

public static void downFileAsyn(final Activity ctx, final String upgradeUrl, final String savedPath,
        final MyCallback<Boolean> callback) {
    final ProgressDialog xh_pDialog = new ProgressDialog(ctx);
    // ?/* w  ww . j av  a  2 s. c o m*/
    xh_pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    // ProgressDialog 
    xh_pDialog.setTitle("???");
    // ProgressDialog???
    xh_pDialog.setMessage("...");
    // ProgressDialog
    // xh_pDialog.setIcon(R.drawable.img2);
    // ProgressDialog ??? false ??
    xh_pDialog.setIndeterminate(false);
    // ProgressDialog ?
    // xh_pDialog.setProgress(100);
    // ProgressDialog ???
    xh_pDialog.setCancelable(true);
    // ProgressDialog
    xh_pDialog.show();

    new Thread() {
        public void run() {

            boolean downloadSuccess = true;
            FileOutputStream fileOutputStream = null;
            try {
                Looper.prepare();
                HttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(upgradeUrl);

                File f = new File(savedPath);
                if (!f.exists()) {
                    f.createNewFile();
                }

                HttpResponse response = client.execute(get);
                HttpEntity entity = response.getEntity();
                // ?
                Long length = entity.getContentLength();
                xh_pDialog.setMax(length.intValue());
                //
                InputStream is = entity.getContent();
                fileOutputStream = null;

                if (is != null && length > 0) {

                    fileOutputStream = new FileOutputStream(f);

                    byte[] buf = new byte[1024];
                    int ch = -1;
                    int count = 0;
                    while ((ch = is.read(buf)) != -1) {

                        if (xh_pDialog.isShowing()) {
                            fileOutputStream.write(buf, 0, ch);
                            // ?
                            count += ch;
                            xh_pDialog.setProgress(count);
                        } else {
                            downloadSuccess = false;
                            break;// ?
                        }
                    }

                } else {
                    callback.onCallback(false);
                }

                if (downloadSuccess && fileOutputStream != null) {
                    xh_pDialog.dismiss();
                    fileOutputStream.flush();
                    fileOutputStream.close();
                    callback.onCallback(true);// ?
                }
                Looper.loop();
            } catch (FileNotFoundException e) {
                xh_pDialog.dismiss();
                e.printStackTrace();
                callback.onCallback(false);
                xh_pDialog.dismiss();
            } catch (Exception e) {
                xh_pDialog.dismiss();
                e.printStackTrace();
                callback.onCallback(false);
            } finally {
                try {
                    fileOutputStream.flush();
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }.start();

}

From source file:com.sakadream.sql.SQLConfigJson.java

/**
 * Save SQLConfig to config.json//from w  w w. j  a v a2 s.  c o m
 * @param sqlConfig SQLConfig object
 * @param encrypt Are you want to encrypt config.json?
 * @throws Exception
 */
public static void save(SQLConfig sqlConfig, Boolean encrypt) throws Exception {
    File file = new File(path);
    if (!file.exists()) {
        file.createNewFile();
    } else {
        FileChannel outChan = new FileOutputStream(file, true).getChannel();
        outChan.truncate(0);
        outChan.close();
    }
    FileWriter writer = new FileWriter(file);
    String json = gson.toJson(sqlConfig, type);
    if (encrypt) {
        Security security = new Security();
        writer.write(security.encrypt(json));
    } else {
        writer.write(json);
    }
    writer.close();
}

From source file:de.matzefratze123.staffinformer.util.I18N.java

private static void copyLanguageXml(File destFile) {
    InputStream inStream = null;/*from w  w  w .  j av  a 2s  .  c o  m*/
    OutputStream outStream = null;

    try {
        destFile.createNewFile();

        inStream = I18N.class.getResourceAsStream('/' + MESSAGES_FILE);
        outStream = new FileOutputStream(destFile);

        final byte[] BUFFER = new byte[1024];
        int read;

        while ((read = inStream.read(BUFFER)) > 0) {
            outStream.write(BUFFER, 0, read);
        }
    } catch (IOException e) {
        Logger.severe("Failed to copy messages file! Using default messages...");
        e.printStackTrace();
    } finally {
        try {
            if (inStream != null) {
                inStream.close();
            }

            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
        } catch (Exception e) {
        }
    }
}

From source file:Main.java

public static void copyFile(File from, File to) {
    if (null == from || !from.exists()) {
        return;/*from www .  j  a  va 2 s .  co m*/
    }
    if (null == to) {
        return;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(from);
        if (!to.exists()) {
            to.createNewFile();
        }
        os = new FileOutputStream(to);

        byte[] buffer = new byte[1024];
        int len = 0;
        while (-1 != (len = is.read(buffer))) {
            os.write(buffer, 0, len);
        }
        os.flush();
    } catch (Exception e) {
    } finally {
        closeIO(is, os);
    }

}

From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java

public static void copyTest(File source, File target) throws Exception {
    FileInputStream fis = null;/*w  ww .  jav a2s .  co  m*/
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(target);
        target.createNewFile();

        byte[] bytes = new byte[16 * 1024];
        int n = -1;
        while ((n = fis.read(bytes, 0, bytes.length)) > 0) {
            fos.write(bytes, 0, n);
        }

        fos.flush();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}

From source file:de.matzefratze123.heavyspleef.util.I18NNew.java

private static void copyLanguageXml(File destFile) {
    InputStream inStream = null;/*  w w  w  .  j  a v  a 2  s  . com*/
    OutputStream outStream = null;

    try {
        destFile.createNewFile();

        inStream = I18NNew.class.getResourceAsStream('/' + MESSAGES_FILE);
        outStream = new FileOutputStream(destFile);

        final byte[] BUFFER = new byte[1024];
        int read;

        while ((read = inStream.read(BUFFER)) > 0) {
            outStream.write(BUFFER, 0, read);
        }
    } catch (IOException e) {
        Logger.severe("Failed to copy messages file! Using default messages...");
        e.printStackTrace();
    } finally {
        try {
            if (inStream != null) {
                inStream.close();
            }

            if (outStream != null) {
                outStream.flush();
                outStream.close();
            }
        } catch (Exception e) {
        }
    }
}

From source file:it.iit.genomics.cru.bridges.liftover.ws.LiftOverRun.java

public static Mapping runLiftOver(String genome, String fromAssembly, String toAssembly, String chromosome,
        int start, int end) {

    String liftOverCommand = RESOURCE_BUNDLE.getString("liftOverCommand");

    String liftOverPath = RESOURCE_BUNDLE.getString("liftOverPath");

    String mapChainDir = RESOURCE_BUNDLE.getString("mapChainDir");

    String tmpDir = RESOURCE_BUNDLE.getString("tmpDir");

    Mapping mapping = null;/*w  ww  .  j  a  v  a 2 s  .c  om*/

    Runtime r = Runtime.getRuntime();

    String rootFilename = String.format("%s", RandomStringUtils.randomAlphanumeric(8));

    String inputFilename = rootFilename + "-" + fromAssembly + ".bed";
    String outputFilename = rootFilename + "-" + toAssembly + ".bed";
    String unmappedFilename = rootFilename + "-" + "unmapped.bed";

    String mapChain = fromAssembly.toLowerCase() + "To" + toAssembly.toUpperCase().charAt(0)
            + toAssembly.toLowerCase().substring(1) + ".over.chain.gz";

    try {

        File tmpDirFile = new File(tmpDir);

        // if the directory does not exist, create it
        if (false == tmpDirFile.exists()) {
            System.out.println("creating directory: " + tmpDir);
            boolean result = tmpDirFile.mkdir();

            if (result) {
                System.out.println("DIR created");
            }
        }

        // Write input bed file
        File inputFile = new File(tmpDir + inputFilename);
        // if file doesnt exists, then create it
        if (!inputFile.exists()) {
            inputFile.createNewFile();
        }

        FileWriter fw = new FileWriter(inputFile.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(chromosome + "\t" + start + "\t" + end + "\n");
        bw.close();

        String commandArgs = String.format("%s %s %s %s %s", liftOverPath + "/" + liftOverCommand,
                tmpDir + inputFilename, mapChainDir + mapChain, tmpDir + outputFilename,
                tmpDir + unmappedFilename);
        System.out.println(commandArgs);

        Process p = r.exec(commandArgs);

        p.waitFor();

        BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = "";

        while ((line = b.readLine()) != null) {
            System.out.println(line);
        }
        b.close();

        b = new BufferedReader(new FileReader(tmpDir + outputFilename));

        while ((line = b.readLine()) != null) {
            String[] cells = line.split("\t");
            String newChromosome = cells[0];
            int newStart = Integer.parseInt(cells[1]);
            int newEnd = Integer.parseInt(cells[2]);
            mapping = new Mapping(genome, toAssembly, newChromosome, newStart, newEnd);
        }
        b.close();

        // delete
        File delete = new File(tmpDir + inputFilename);
        delete.delete();

        delete = new File(tmpDir + outputFilename);
        delete.delete();

        delete = new File(tmpDir + unmappedFilename);
        delete.delete();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    return mapping;
}

From source file:Main.java

/** Writes the entire InputStream into the destination file.
 * @param in the input stream to write/*w w w. j  a  v  a  2  s  . co m*/
 * @param dest the file to write to.
 */
public static void write(InputStream in, File dest) throws IOException {
    OutputStream out = null;
    try {
        if (dest.exists() == false)
            dest.createNewFile();

        out = new FileOutputStream(dest);
        write(in, out);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Throwable t) {
            }
        }
    }
}

From source file:com.atlassian.clover.eclipse.core.licensing.LicenseUtils.java

public static boolean writeLicenseTo(File licenseFile) throws IOException {
    String licenseText = CloverPlugin.getInstance().getInstallationSettings().getLicenseText();

    if (licenseFile != null) {
        if (!licenseFile.exists()) {
            licenseFile.createNewFile();
        }//from www .j  av a2 s  .co m
        FileWriter writer = new FileWriter(licenseFile);
        writer.write(licenseText);
        writer.flush();
        writer.close();
        return true;
    } else {
        return false;
    }
}

From source file:Main.java

public static void loadHashMapData() throws IOException {
    HashMap<String, String> hashMap = new HashMap<String, String>();
    Properties properties = new Properties();

    File file = new File(HASHMAPFILEPATH);
    if (file.exists()) {
        properties.load(new FileInputStream(HASHMAPFILEPATH));

        for (String key : properties.stringPropertyNames()) {
            hashMap.put(key, properties.getProperty(key));
            GPSDataMap = hashMap;// ww w  .j a  v  a  2 s .  c  om
        }
    } else {
        file.createNewFile();
    }
}