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.manning.androidhacks.hack037.MediaUtils.java

public static void saveRaw(Context context, int raw, String path) {
    File completePath = new File(Environment.getExternalStorageDirectory(), path);

    try {//from   w  w w.j  a v a2s .  co m
        completePath.getParentFile().mkdirs();
        completePath.createNewFile();

        BufferedOutputStream bos = new BufferedOutputStream((new FileOutputStream(completePath)));
        BufferedInputStream bis = new BufferedInputStream(context.getResources().openRawResource(raw));
        byte[] buff = new byte[32 * 1024];
        int len;
        while ((len = bis.read(buff)) > 0) {
            bos.write(buff, 0, len);
        }
        bos.flush();
        bos.close();

    } catch (IOException io) {
        Log.e(TAG, "Error: " + io);
    }
}

From source file:mobac.program.EnvironmentSetup.java

protected static void checkDirectory(File dir, String dirName, boolean checkIsWriteable) {
    try {//  www  .jav a2 s  .  c om
        Utilities.mkDirs(dir);
    } catch (IOException e) {
        GUIExceptionHandler.processFatalExceptionSimpleDialog("Error while creating " + dirName + " directory\n"
                + dir.getAbsolutePath() + "\nProgram will exit.", e);
    }
    if (!checkIsWriteable)
        return;
    try {
        // test if we can write into that directory
        File testFile = File.createTempFile("MOBAC", "", dir);
        testFile.createNewFile();
        testFile.deleteOnExit();
        testFile.delete();
    } catch (IOException e) {
        GUIExceptionHandler.processFatalExceptionSimpleDialog("Unable to write to " + dirName + "\n"
                + dir.getAbsolutePath() + "\nPlease correct file permissions and restart MOBAC", e);
    }
}

From source file:Main.java

public static void logProgressReport(Context c, String[] logs) {
    String dir = c.getExternalFilesDir(null).getParent();
    try {/*from   w  w w.j  a  v a 2 s.c  om*/
        File file = new File(dir + "/sf_reports.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 logProgressReport!");
        ioe.printStackTrace();
    }
}

From source file:com.github.wesjd.overcastmappacker.xml.DocumentHandler.java

public static DocumentHandler createNewXMLFile(File file) {
    try {//from   w w  w .ja va2  s  .c om
        if (!file.exists()) {
            if (!file.createNewFile())
                throw new RuntimeException("Unable to create new XML file.");
            Files.write(Paths.get(file.getAbsolutePath()), XMLConstants.DEFAULT_LINES);
        }
        return new DocumentHandler(file);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:cz.matfyz.oskopek.learnr.tools.DatasetIO.java

/**
 * Saves the dataset via XStream./* w ww  .j  av  a2 s .co  m*/
 * <p/>
 * Used for saving a used dataset. Saves statistics.
 *
 * @param dataset  the dataset to save
 * @param filename the filename into which to save
 * @throws IOException if an error during write occurs
 */
public static void saveXMLDataset(Dataset dataset, String filename) throws IOException {
    LOGGER.debug("Save dataset to XML: \'{}\'", filename);
    File outFile = new File(filename);
    outFile.createNewFile(); // Ignored return value, checking for overwriting in UI
    FileOutputStream fileOutputStream = new FileOutputStream(outFile);

    XStream xStream = new XStream();
    xStream.toXML(dataset, fileOutputStream);

    fileOutputStream.close();
}

From source file:net.orpiske.ssps.common.repository.RepositorySettings.java

private static void addUserConfig(final RepositoryInfo repositoryInfo) throws RepositorySetupException {
    String repositoryPath = RepositoryUtils.getUserRepository();

    String path = repositoryPath + File.separator + repositoryInfo.getName() + File.separator + "user.conf";

    File file = new File(path);

    if (!file.exists()) {
        try {//from ww  w.j  av  a  2  s  .com
            file.createNewFile();
        } catch (IOException e) {
            throw new RepositorySetupException("Unable to create user configuration " + "file", e);
        }
    } else {
        throw new RepositorySetupException("This user already exists");
    }

    PropertiesConfiguration userConfig;
    try {
        userConfig = new PropertiesConfiguration(path);

        userConfig.addProperty(repositoryInfo.getName() + ".auth.user", repositoryInfo.getUserName());
        userConfig.addProperty(repositoryInfo.getName() + ".auth.password", repositoryInfo.getPassword());
        userConfig.save();
    } catch (ConfigurationException e) {
        throw new RepositorySetupException(
                "Unable to save user configuration to " + path + ":" + e.getMessage(), e);
    }

}

From source file:Main.java

public static void appendLog(String text) {
    File logFile = new File(Environment.getExternalStorageDirectory() + "/clr_log.file");
    if (!logFile.exists()) {
        try {/*from   w w  w. j  ava2 s. c om*/
            logFile.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    try {
        BufferedReader buf = new BufferedReader(new FileReader(logFile));

        String line = "";
        ArrayList<String> lines = new ArrayList<String>();

        while ((line = buf.readLine()) != null) {
            lines.add(line);
        }

        int size = lines.size();

        //Every time the number of lines go over 1000, only add the last 500. This will keep its size to a minimum
        int i = lines.size() - 500;
        if (i < 0) {
            i = 0;
        }

        buf.close();

        BufferedWriter bufW = new BufferedWriter(new FileWriter(logFile, true));

        if (size > 1000) {
            bufW.write("");
            for (; i < lines.size(); i++) {
                bufW.append(line);
            }
        }

        bufW.append(text + "\n");
        bufW.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static boolean writeToFileAppend(@NonNull File file, @NonNull CharSequence text) {
    try {/*from   www  . j  av  a2 s. c  o  m*/
        //noinspection ResultOfMethodCallIgnored
        file.createNewFile();
    } catch (IOException e) {
        Log.w(TAG, "Failed to create the file: file=" + file.getName());
        return false;
    }

    OutputStream os = null;
    InputStream is = null;
    try {
        os = new FileOutputStream(file, true);
        is = new ByteArrayInputStream(text.toString().getBytes("UTF-8"));

        int read;
        final byte[] buffer = new byte[1024];
        do {
            read = is.read(buffer, 0, buffer.length);
            if (read > 0)
                os.write(buffer, 0, read);
        } while (read > 0);
        return true;
    } catch (Exception e) {
        Log.w(TAG, "Failed to append to a file: file=" + file.getName());
        e.printStackTrace();
    } finally {
        // Try to close the stream.
        if (os != null)
            try {
                os.close();
            } catch (IOException e) {
                Log.e(TAG, "Failed to close the stream!");
                e.printStackTrace();
            }

        // Try to close the stream.
        if (is != null)
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "Failed to close the stream!");
                e.printStackTrace();
            }
    }
    return false;
}

From source file:com.exalttech.trex.util.files.FileManager.java

/**
 * Create new file//from   www  .  java  2s. c  o m
 *
 * @param fileName
 * @return
 * @throws IOException
 */
public static File createNewFile(String fileName) throws IOException {
    String path = getProfilesFilePath() + fileName;
    File newFile = new File(path);
    newFile.createNewFile();
    return newFile;
}

From source file:Main.java

public static File getFontDir(Context context) {

    File dir = null;/*  w ww. j  a  v  a2s . c  o m*/
    dir = new File(context.getCacheDir() + File.separator + "wanjia" + File.separator + "font");
    File nomediaFile = new File(dir.getAbsolutePath() + File.separator + ".nomedia");
    if (!dir.exists()) {
        dir.mkdirs();
    }
    if (!nomediaFile.exists()) {
        try {
            nomediaFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return dir;
}