Java Text File Write nio writeToFile(String content, String fileName)

Here you can find the source of writeToFile(String content, String fileName)

Description

Creates file under temporary file directory and writes configuration to it.

License

LGPL

Parameter

Parameter Description
content a parameter
fileName a parameter

Return

created file

Declaration

public static synchronized File writeToFile(String content,
        String fileName) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.BufferedWriter;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

public class Main {
    /**/*from  w ww.  java 2 s .  c  om*/
     * Creates file under temporary file directory and writes configuration to
     * it. Returns the created file.
     * 
     * @param content
     * @param fileName
     * @return created file
     */
    public static synchronized File writeToFile(String content,
            String fileName) {
        File temp = null;
        BufferedWriter bw = null;
        try {
            temp = new File(System.getProperty("java.io.tmpdir")
                    + File.separator + fileName);
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(temp), StandardCharsets.UTF_8));
            bw.write(content);
            bw.flush();
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e1) {
            e1.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return temp;
    }
}

Related

  1. writeTextFile(String file, String articleContent)
  2. writeTo(File file, String content)
  3. writeToFile(File file, String content)
  4. writeToFile(File file, String str)
  5. writeToFile(final File file, final String data)
  6. writeToFile(String content, String fileName, boolean createFile)
  7. writeToFile(String filename, String fileContents)
  8. writeToFile(String filename, String... lines)
  9. writeTokenText(OutputStream theOs, String theStr)