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:me.neatmonster.spacertk.scheduler.Scheduler.java

/**
 * Saves all the queued jobs to the file
 *///from   w  ww.  j av a2s.  com
public static void saveJobs() {
    final File file = new File(SpaceModule.MAIN_DIRECTORY, "jobs.yml");
    if (!file.exists())
        try {
            file.createNewFile();
        } catch (final IOException e) {
            e.printStackTrace();
        }
    final YamlConfiguration configuration = YamlConfiguration.loadConfiguration(file);
    for (final String jobName : jobs.keySet()) {
        final Job job = jobs.get(jobName);
        final String actionName = job.actionName;
        final List<Object> actionArguments = Arrays.asList(job.actionArguments);
        final String timeType = job.timeType;
        final String timeArgument = job.timeArgument;
        configuration.set(jobName + ".TimeType", timeType);
        configuration.set(jobName + ".TimeArgument", timeArgument);
        configuration.set(jobName + ".ActionName", actionName);
        configuration.set(jobName + ".ActionArguments", JSONValue.toJSONString(actionArguments)
                .replace("[[", "[").replace("],[", ",").replace("]]", "]"));
    }
    try {
        configuration.save(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:edu.isi.karma.util.FileUtil.java

public static void copyFiles(File destination, File source) throws FileNotFoundException, IOException {
    if (!destination.exists()) {
        destination.createNewFile();
    }/* w  ww  .ja  va  2 s  .  c o m*/
    InputStream in = new FileInputStream(source);
    OutputStream out = new FileOutputStream(destination);

    byte[] buf = new byte[1024];
    int len;

    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
    logger.debug("Done copying contents of " + source.getName() + " to " + destination.getName());
}

From source file:com.frostwire.android.gui.util.SystemUtils.java

public static File getTempDirectory() {
    File f = createFolder(getApplicationStorageDirectory(), TEMP_FOLDER_NAME);

    File nomedia = new File(f, ".nomedia");
    if (!nomedia.exists()) {
        try {/*from   www.j a  v  a  2 s.  c  om*/
            nomedia.createNewFile();
        } catch (IOException e) {
            // unable to create nomedia file, ignore it for now
        }
    }

    return f;
}

From source file:gov.va.oia.terminology.converters.sharedUtils.Unzip.java

public final static void unzip(File zipFile, File rootDir) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        java.io.File f = new java.io.File(rootDir, entry.getName());
        if (entry.isDirectory()) {
            f.mkdirs();// w  ww.java2  s .co m
            continue;
        } else {
            f.createNewFile();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = zip.getInputStream(entry);
            os = new FileOutputStream(f);
            IOUtils.copy(is, os);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    // noop
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    // noop
                }
            }
        }
    }
    zip.close();
}

From source file:com.a544jh.kanamemory.io.JsonFileWriter.java

/**
 * Saves the PlayerProfile to a JSON-formatted file. If the file exists, the
 * profile is added to the existing JSON-mapping, if a profile with the same
 * name already exists in th file, it will be overwritten. If the files does
 * not exist, a new JSON-formatted file will be created.
 *
 * @param profile The PlayerProfile to be saved.
 * @param filename The file to save the data to. Can be a JSON-formatted
 * file created by this method, or an nonexistent file.
 *//*from  ww  w .j av  a 2  s.  c  o  m*/
public static void saveProfile(PlayerProfile profile, String filename) {
    File file = new File(filename);
    JSONObject jo = null;

    //Create a new file if it doesn't exist
    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException ex) {
            Logger.getLogger(JsonFileWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
        //Create new empty JSONObject
        jo = new JSONObject();
    } else {
        //If the file exists
        try {
            //Read the JSONObject from the file
            jo = JsonFileReader.readFileToJsonObject(file);
        } catch (FileNotFoundException | JSONException ex) {
            Logger.getLogger(JsonFileWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    try {
        //Put the profile's scores to be saved in the JSONOBject with the name as key
        jo.put(profile.getName(), profile.getScoresMap());
        //Write the actual file
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(jo.toString(4));
        }
    } catch (JSONException | IOException ex) {
        Logger.getLogger(JsonFileWriter.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:fiftyone.mobile.detection.webapp.ImageCache.java

/**
 * Adds the image at the width and height provided to the cache.
 * @param imageLocal//w  w w. ja  v a2s  . com
 * @param width
 * @param height
 * @param imageAsStream
 * @throws IOException 
 */
synchronized static void add(File physicalPath, File cacheFile, int width, int height,
        InputStream imageAsStream) throws IOException {
    new File(cacheFile.getParent()).mkdirs();
    cacheFile.createNewFile();
    OutputStream outputStream = new FileOutputStream(cacheFile);

    int read = 0;
    byte[] bytes = new byte[1024 ^ 2];

    while ((read = imageAsStream.read(bytes)) != -1) {
        outputStream.write(bytes, 0, read);
    }

    outputStream.close();
}

From source file:io.specto.hoverfly.junit.HoverflyRuleUtils.java

static URI createFileRelativeToClasspath(String resourceName) throws IOException {
    final File file = Paths.get(new File("").getAbsolutePath(), resourceName).toFile();
    file.mkdirs();/*from   w  w w  . j av a  2  s. c om*/
    if (!file.exists()) {
        file.delete();
    }
    file.createNewFile();
    return file.toURI();
}

From source file:Main.java

public static void saveBitmap(String dirpath, String filename, Bitmap bitmap, boolean isDelete) {
    File dir = new File(dirpath);
    if (!dir.exists()) {
        dir.mkdirs();/*  ww  w  .j  a v  a  2s  . co m*/
    }

    File file = new File(dirpath, filename);
    if (isDelete) {
        if (file.exists()) {
            file.delete();
        }
    }

    if (!file.exists()) {
        try {
            file.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(file);
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)) {
            out.flush();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static String imageToLocal(Bitmap bitmap, String name) {
    String path = null;/*from w ww .ja  v  a 2s.  c  o  m*/
    try {
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return null;
        }
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/cache/";
        File file = new File(filePath, name);
        if (file.exists()) {
            path = file.getAbsolutePath();
            return path;
        } else {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();

        OutputStream outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }

        path = file.getAbsolutePath();
    } catch (Exception e) {
    }
    return path;
}

From source file:net.orpiske.sdm.lib.io.IOUtil.java

/**
 * Protects a file or resource from being written
 * @param resource the resource to protect
 * @throws IOException/*from  w  w  w .j a  va  2s.  c om*/
 */
public static void shield(final String resource) throws IOException {
    File shieldedFile = new File(resource);

    if (!shieldedFile.exists()) {
        System.out.println("Resource " + resource + " does not exist");

        return;
    }

    File shielded = new File(resource + ShieldUtils.SHIELD_EXT);

    if (!shielded.exists()) {

        if (!shielded.createNewFile()) {
            System.err.println("Unable to create shield file " + shielded.getPath());
        }

        System.out.println("Resource " + resource + " was shielded");
    } else {
        System.out.println("Resource " + resource + " already shielded");
    }

    shielded.deleteOnExit();
}