Java Write String to File writeFile(String fileName, String content, String encoding)

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

Description

write File

License

Open Source License

Declaration

public static void writeFile(String fileName, String content, String encoding) throws IOException 

Method Source Code


//package com.java2s;
/*/*from ww w.ja  v  a  2s  .c o  m*/
 * Copyright (c) 2012-2016 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

import java.io.File;

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

import java.io.OutputStreamWriter;

public class Main {
    public static void writeFile(String fileName, String content) throws IOException {
        writeFile(fileName, content, null);
    }

    public static void writeFile(String fileName, String content, String encoding) throws IOException {
        File f = new File(fileName);
        FileOutputStream fos = new FileOutputStream(f);
        OutputStreamWriter osw;
        if (encoding != null) {
            osw = new OutputStreamWriter(fos, encoding);
        } else {
            osw = new OutputStreamWriter(fos);
        }

        try {
            osw.write(content);
        } finally {
            osw.close();
        }
    }
}

Related

  1. writeFile(String filename, String content)
  2. writeFile(String fileName, String content)
  3. WriteFile(String filename, String content)
  4. writeFile(String filename, String content)
  5. writeFile(String filename, String content)
  6. writeFile(String fileName, String contents)
  7. writeFile(String fileName, String contents)
  8. writeFile(String filename, String contents)
  9. writeFile(String fileName, String contentStr, String charset)