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.sakadream.sql.SQLConfigJson.java

/**
 * Save SQLConfig to custom file name/*  w ww.ja  v  a  2s  .  c  o m*/
 * @param fileName SQLConfig file path
 * @param sqlConfig SQLConfig object
 * @param encrypt Are you want to encrypt config.json?
 * @throws Exception
 */
public static void save(String fileName, SQLConfig sqlConfig, Boolean encrypt) throws Exception {
    fileName = Utilis.changeFileName(fileName, "json");
    File file = new File(fileName);
    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:com.adaptris.core.services.metadata.SimpleSequenceNumberService.java

private static Properties load(File myFile) throws IOException {
    if (!myFile.exists()) {
        myFile.createNewFile();
    }/*from   ww w  . ja  v  a2 s .  c  om*/
    return PropertyHelper.loadQuietly(myFile);
}

From source file:fr.eo.util.dumper.Dumper.java

private static BufferedWriter getWriter(String requestName, int cpt) throws IOException {
    String path = "", fileName = "";

    if (cpt > 1) {
        fileName = "dump_" + requestName.toLowerCase().replaceAll(" ", "_") + "_" + cpt + ".sql";
    } else {//www .  ja  v a  2s  . c  o m
        fileName = "dump_" + requestName.toLowerCase().replaceAll(" ", "_") + ".sql";
    }

    dumpFileNames.add(fileName);

    path = assetFolder + "/" + fileName;
    File dumpFile = new File(path);

    System.out.println("Getting writer for file : '" + dumpFile.getAbsolutePath() + "'");

    dumpFile.createNewFile();

    return new BufferedWriter(new FileWriter(dumpFile));
}

From source file:io.github.thefishlive.updater.GitUpdater.java

public static void loadConfig() {
    File config = new File("config.cfg");

    if (!config.exists()) {
        try {//w  w w . j ava 2s  .  c  o m
            InputStream stream = GitUpdater.class.getResourceAsStream("config.cfg");

            if (stream != null) {
                FileUtils.copyInputStreamToFile(stream, config);
            } else {
                config.createNewFile();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    String line = "";

    try {
        BufferedReader reader = new BufferedReader(new FileReader(config));
        while ((line = reader.readLine()) != null) {
            if (line.startsWith("gitrepo:")) {
                gitrepo = new File(line.substring("gitrepo:".length()).trim());
                if (!gitrepo.exists()) {
                    gitrepo.mkdirs();
                }
            } else if (line.startsWith("remote:")) {
                remote = line.substring("remote:".length()).trim();
            } else if (line.startsWith("port:")) {
                port = Integer.parseInt(line.substring("port:".length()).trim());
            }
        }
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.mirth.connect.util.AttachmentUtil.java

public static void writeToFile(String filePath, Attachment attachment, boolean binary) throws IOException {
    File file = new File(filePath);
    if (!file.canWrite()) {
        String dirName = file.getPath();
        int i = dirName.lastIndexOf(File.separator);
        if (i > -1) {
            dirName = dirName.substring(0, i);
            File dir = new File(dirName);
            dir.mkdirs();//from w  ww.  ja v a 2 s. co  m
        }
        file.createNewFile();
    }

    if (attachment != null && StringUtils.isNotEmpty(filePath)) {
        FileUtils.writeByteArrayToFile(file,
                binary ? Base64Util.decodeBase64(attachment.getContent()) : attachment.getContent());
    }
}

From source file:edu.du.penrose.systems.util.SendMail.java

/**
 * Sends email to the email success addresses contained in batchOptions. The ingest report and the pid report are
 * contained within a single email. If the eamil addresses is not set no report is sent.
 * /* ww w.j a  va  2  s . c  om*/
 * @param batchOptions
 * @param String ingestReportPath
 * @param String pidReportPath
 * @throws Exception
 */
public static void sendReportEmail(BatchIngestOptions batchOptions, String ingestReportPath,
        String pidReportPath) throws Exception {
    String[] recipientArray = new String[2];

    recipientArray[0] = batchOptions.getSuccessEmail();
    recipientArray[1] = batchOptions.getSuccessEmail_2();

    if (batchOptions.getStmpHost() == null || batchOptions.getStmpHost().length() == 0) {
        return;
    }
    if (batchOptions.getStmpPort() == null || batchOptions.getStmpPort().length() == 0) {
        return;
    }
    if (batchOptions.getStmpUser() == null || batchOptions.getStmpUser().length() == 0) {
        return;
    }
    if (batchOptions.getStmpPassword() == null || batchOptions.getStmpPassword().length() == 0) {
        return;
    }
    if (batchOptions.getEmailFromAddress() == null || batchOptions.getEmailFromAddress().length() == 0) {
        return;
    }

    String smptServerHost = batchOptions.getStmpHost().trim();
    String smptServerPort = batchOptions.getStmpPort().trim();
    String smtpUser = batchOptions.getStmpUser().trim();
    String smtpPassword = batchOptions.getStmpPassword().trim();
    boolean sslEmail = batchOptions.isStmpUseSSL();
    String fromAddress = batchOptions.getEmailFromAddress().trim();

    /**
     * createNewFile() creates an empty file ONLY IF IT DOESN"T ALREADY EXIST, this only happens during an error condition.
     */
    File ingestReportFile = new File(ingestReportPath);
    ingestReportFile.createNewFile(); // see above
    File pidReportFile = new File(pidReportPath);
    pidReportFile.createNewFile(); // see above   

    StringBuffer emailMessage = null;
    String line = "";

    BufferedReader ingestReportReader = new BufferedReader(new FileReader(ingestReportFile));
    BufferedReader pidReportReader = new BufferedReader(new FileReader(pidReportFile));
    emailMessage = new StringBuffer();
    while ((line = ingestReportReader.readLine()) != null) {
        emailMessage.append(line + "\n");
    }
    emailMessage.append("\n\n");
    line = pidReportReader.readLine();
    while (line != null) {
        emailMessage.append(line.replaceAll(",", ", ") + "\n");
        line = pidReportReader.readLine();
    }

    postMailWithAuthenication(recipientArray, batchOptions.getBatchSetName() + " Ingest Report",
            emailMessage.toString(), fromAddress, smptServerHost, smtpUser, smtpPassword, smptServerPort,
            sslEmail);

    logger.info(batchOptions.getBatchSetName() + " Remote Ingest report sent");
}

From source file:com.insightml.utils.io.IoUtils.java

public static void append(final File file, final String text) {
    try {//ww w .  ja v a2s.c om
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            file.createNewFile();
        }
        try (FileWriter fw = new FileWriter(file, true); BufferedWriter out = new BufferedWriter(fw)) {
            out.write(text);
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:com.ruesga.rview.misc.CacheHelper.java

public static File createNewTemporaryFile(Context context, String suffix) throws IOException {
    String ts = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
    File storageDir = context.getFilesDir();
    if (storageDir.exists() || storageDir.mkdirs()) {
        File file = new File(storageDir.getAbsolutePath(), ts + suffix);
        if (file.createNewFile()) {
            return new File(storageDir.getAbsolutePath(), ts + suffix);
        }/*from  ww w .  j a  va 2s.  c om*/
    }
    return null;
}

From source file:com.tealcube.minecraft.bukkit.facecore.utilities.IOUtils.java

/**
 * Create a {@link java.io.File} from the given {@link java.io.File}.
 *
 * @param file File to create// w w w  . j av a2s  .  co  m
 * @return if creation was successful
 */
public static boolean createFile(File file) {
    Validate.notNull(file, "file cannot be null");
    boolean succeeded = file.exists();
    if (!succeeded) {
        try {
            if (!createDirectory(file.getParentFile())) {
                return false;
            }
            succeeded = file.createNewFile();
        } catch (IOException ignored) {
            // do nothing here
        }
    }
    return succeeded;
}

From source file:commonUtils.FunctionUtils.java

public static boolean WriteTextFile(String pathFileText, String fileContent) {
    try {//from   w  ww  .  j  a v  a 2 s.  c o  m
        File file = new File(pathFileText);

        //if file doesnt exists, then create it
        if (!file.exists()) {
            file.createNewFile();
        }

        //Collecting Data
        String fileDat = "";
        if (!IsNullOrEmpty(fileContent)) {
            String[] listContent = StringUtils.split(fileContent, "\n");
            fileDat += listContent.length + "\n";

            for (String tmpPost : listContent) {
                if (tmpPost != null) {
                    fileDat += tmpPost + "\n";
                }
            }
            //true = append file
            FileWriter fileWritter = new FileWriter(file.getPath(), false);
            BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
            bufferWritter.write(fileDat);
            bufferWritter.close();

            return true;
        }
    } catch (IOException ex) {
        return false;
    }
    return false;
}