Java Text File Save writeFile(String path, String fileName, String content, String charset)

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

Description

write File

License

Apache License

Declaration

public static String writeFile(String path, String fileName, String content, String charset)
            throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    public static String writeFile(String path, String fileName, String content) throws IOException {
        return writeFile(path, fileName, content, "UTF-8");
    }//w w w  .  j  av a 2  s . c  o m

    public static String writeFile(String path, String fileName, String content, String charset)
            throws IOException {
        mkDir(path);
        String filePath = path + File.separator + fileName;
        File file = new File(filePath);
        if (file.exists()) {
            file.delete();
        }

        file.createNewFile();
        FileOutputStream fops = new FileOutputStream(file);
        fops.write(content.getBytes(charset));
        fops.flush();
        fops.close();
        return filePath;
    }

    public static void mkDir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

    }
}

Related

  1. writeFile(String path, String content)
  2. writefile(String path, String content, boolean append)
  3. writeFile(String path, String contents)
  4. writeFile(String path, String data)
  5. writeFile(String path, String fileName, String content)
  6. writeFile(String path, String fileName, String data)
  7. writeFile(String path, String text)
  8. writeFile(String path, String text)
  9. writeFile(String path, String text, boolean orverWite)