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:de.dakror.scpuller.SCPuller.java

public static void saveDownloadedSongs() {
    File f = new File(System.getProperty("user.home") + "/.dakror/SCPuller/downloaded.txt");
    try {/*from   w ww  .j  a  va2  s. c  om*/
        f.createNewFile();
        BufferedWriter bw = new BufferedWriter(new FileWriter(f));
        for (int i : downloadedSongs)
            bw.write(i + "\r\n");

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

From source file:Main.java

private static void saveFileForLocal(String requestPath, String result) {
    // TODO Auto-generated method stub
    File file = new File(requestPath);
    if (!file.exists()) {
        try {/*from   w  w  w  . ja v a2  s . co  m*/
            File parentFile = file.getParentFile();
            if (!parentFile.exists()) {
                parentFile.mkdirs();
            }
            file.createNewFile();
            FileOutputStream fout = new FileOutputStream(file);
            byte[] buffer = result.getBytes();
            fout.write(buffer);
            fout.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:com.github.seqware.queryengine.system.Utility.java

/**
 * Check whether we can create the output for a particular filename
 *
 * @param filename a {@link java.lang.String} object.
 * @return reference to the newly created output file
 * @throws java.io.IOException if any./*from   ww  w  .j  a  v a2s  .c  o  m*/
 */
public static File checkOutput(String filename) throws IOException {
    File outputFile = new File(filename);
    if (outputFile.exists()) {
        outputFile.delete();
    }
    try {
        outputFile.createNewFile();
    } catch (IOException ex) {
        Logger.getLogger(FeatureImporter.class.getName()).fatal("Could not create output file");
        throw ex;
    }
    return outputFile;
}

From source file:com.notifier.desktop.Main.java

private static boolean getExclusiveExecutionLock() throws IOException {
    File lockFile = new File(OperatingSystems.getWorkDirectory(), Application.ARTIFACT_ID + ".lock");
    Files.createParentDirs(lockFile);
    lockFile.createNewFile();
    final RandomAccessFile randomAccessFile = new RandomAccessFile(lockFile, "rw");
    final FileChannel fileChannel = randomAccessFile.getChannel();
    final FileLock fileLock = fileChannel.tryLock();
    if (fileLock == null) {
        Closeables.closeQuietly(fileChannel);
        Closeables.closeQuietly(randomAccessFile);
        return false;
    }/*from  w  w w .  ja va  2 s .com*/

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                fileLock.release();
            } catch (IOException e) {
                System.err.println("Error releasing file lock");
                e.printStackTrace(System.err);
            } finally {
                Closeables.closeQuietly(fileChannel);
                Closeables.closeQuietly(randomAccessFile);
            }
        }
    });
    return true;
}

From source file:com.streamsets.datacollector.util.ClusterUtil.java

public static void setupCluster(String testName, String pipelineJson, YarnConfiguration yarnConfiguration)
        throws Exception {
    System.setProperty("sdc.testing-mode", "true");
    System.setProperty(MiniSDCTestingUtility.PRESERVE_TEST_DIR, "true");
    yarnConfiguration.set("yarn.nodemanager.delete.debug-delay-sec", "600");
    miniSDCTestingUtility = new MiniSDCTestingUtility();
    File dataTestDir = miniSDCTestingUtility.getDataTestDir();

    //copy spark files under the test data directory into a dir called "spark"
    File sparkHome = ClusterUtil.createSparkHome(dataTestDir);

    //start mini yarn cluster
    miniYarnCluster = miniSDCTestingUtility.startMiniYarnCluster(testName, 1, 1, 1, yarnConfiguration);
    Configuration config = miniYarnCluster.getConfig();

    long deadline = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10);
    while (config.get(YarnConfiguration.RM_ADDRESS).split(":")[1] == "0") {
        if (System.currentTimeMillis() > deadline) {
            throw new IllegalStateException("Timed out waiting for RM to come up.");
        }//  ww  w  .j  a va2 s. co  m
        LOG.debug("RM address still not set in configuration, waiting...");
        TimeUnit.MILLISECONDS.sleep(100);
    }
    LOG.debug("RM at " + config.get(YarnConfiguration.RM_ADDRESS));

    Properties sparkHadoopProps = new Properties();
    for (Map.Entry<String, String> entry : config) {
        sparkHadoopProps.setProperty("spark.hadoop." + entry.getKey(), entry.getValue());
    }

    LOG.debug("Creating spark properties file at " + dataTestDir);
    File propertiesFile = new File(dataTestDir, "spark.properties");
    propertiesFile.createNewFile();
    FileOutputStream sdcOutStream = new FileOutputStream(propertiesFile);
    sparkHadoopProps.store(sdcOutStream, null);
    sdcOutStream.flush();
    sdcOutStream.close();
    // Need to pass this property file to spark-submit for it pick up yarn confs
    System.setProperty(SPARK_PROPERTY_FILE, propertiesFile.getAbsolutePath());

    File sparkBin = new File(sparkHome, "bin");
    for (File file : sparkBin.listFiles()) {
        MiniSDCTestingUtility.setExecutePermission(file.toPath());
    }

    miniSDC = miniSDCTestingUtility.createMiniSDC(MiniSDC.ExecutionMode.CLUSTER);
    miniSDC.startSDC();
    serverURI = miniSDC.getServerURI();
    miniSDC.createPipeline(pipelineJson);
    miniSDC.startPipeline();

    int attempt = 0;
    //Hard wait for 2 minutes
    while (miniSDC.getListOfSlaveSDCURI().size() == 0 && attempt < 24) {
        Thread.sleep(5000);
        attempt++;
        LOG.debug("Attempt no: " + attempt + " to retrieve list of slaves");
    }
    if (miniSDC.getListOfSlaveSDCURI().size() == 0) {
        throw new IllegalStateException("Timed out waiting for slaves to come up.");
    }
}

From source file:com.wabacus.util.FileLockTools.java

public static boolean createLockFile(String lockfile) {
    try {//from w ww .  j  a  v  a2s .  c o m
        File f = new File(lockfile);
        if (f != null && f.isFile()) {
            f.delete();
        }
        return f.createNewFile();
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static void saveImage(String imagePath, byte[] buffer) throws IOException {
    File f = new File(imagePath);
    if (f.exists()) {
        return;//from  w w w.  j  av a 2  s  .co  m
    } else {
        File parentFile = f.getParentFile();
        if (!parentFile.exists()) {
            parentFile.mkdirs();
        }
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(imagePath);
        fos.write(buffer);
        fos.flush();
        fos.close();
    }
}

From source file:Main.java

public static void saveImageToSD(Context ctx, String filePath, Bitmap bitmap, int quality) throws IOException {
    if (bitmap != null) {
        File file = new File(filePath.substring(0, filePath.lastIndexOf(File.separator)));
        if (!file.exists()) {
            file.mkdirs();/*from ww w. j a  v  a2  s .  co  m*/
        }
        file = new File(filePath);
        if (!file.exists()) {
            file.createNewFile();
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        bitmap.compress(CompressFormat.JPEG, quality, bos);
        bos.flush();
        bos.close();
        if (ctx != null) {
            scanPhoto(ctx, filePath);
        }
    }
}

From source file:Main.java

/**
 * Returns a TempFile with given root, filename, and extension.
 * The resulting TempFile is safe for use with Android's MediaRecorder
 * @param c/*w ww . j a v a2s . co  m*/
 * @param root
 * @param filename
 * @param extension
 * @return
 */
public static File createTempFile(Context c, File root, String filename, String extension) {
    File output = null;
    try {
        if (filename != null) {
            if (!extension.contains("."))
                extension = "." + extension;
            output = new File(root, filename + extension);
            output.createNewFile();
            //output = File.createTempFile(filename, extension, root);
            Log.i(TAG, "Created temp file: " + output.getAbsolutePath());
        }
        return output;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static File saveStreamToFile(InputStream inputStream, String path) {
    File file = new File(path);
    if (!file.getParentFile().exists()) {
        //noinspection ResultOfMethodCallIgnored
        file.getParentFile().mkdirs();//w w w. j  a va  2s. com
    }
    try {
        //noinspection ResultOfMethodCallIgnored
        file.createNewFile();

        FileOutputStream outputStream = new FileOutputStream(path);

        int bytesRead;
        byte[] buffer = new byte[1024];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}