Java Write String to File writeFile(String filename, String content)

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

Description

write File

License

Open Source License

Declaration

public static void writeFile(String filename, String content) throws IOException 

Method Source Code


//package com.java2s;

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 {
        File file = new File(filename);
        if (!file.exists()) {
            file.createNewFile();/*  ww w  .  j  a  va  2 s .com*/
        }
        FileOutputStream fos = new FileOutputStream(file);
        OutputStreamWriter writer = null;

        try {
            writer = new OutputStreamWriter(fos, "UTF-8");
            writer.write(content);
            writer.flush();
        } finally {
            if (writer != null)
                writer.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 content)
  7. writeFile(String fileName, String content, String encoding)
  8. writeFile(String filename, String contents)
  9. writeFile(String fileName, String contents)