Java Text File Save writeFile(String file, String text)

Here you can find the source of writeFile(String file, String text)

Description

write File

License

Open Source License

Declaration

public static int writeFile(String file, String text) 

Method Source Code


//package com.java2s;
import java.io.BufferedWriter;
import java.io.File;

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

public class Main {

    public static int writeFile(String file, String text) {
        return writeFile(file, text, null);
    }/*from  ww w .java 2 s. c  o  m*/

    public static int writeFile(String file, String text, String encoding) {
        File f = new File(file);
        if (!f.exists()) {
            return createFile(file, text, encoding);
        } else {
            return appendFile(file, text, encoding);
        }
    }

    public static File createFile(String filePath) {
        File rtn = null;
        try {
            File file = new File(filePath);
            makeParentDir(file);
            file.createNewFile();
            rtn = file;
        } catch (IOException e) {
            rtn = null;
            e.printStackTrace();
        }
        return rtn;
    }

    public static int createFile(String file, String text, String encoding) {
        int rtn = 0;
        FileOutputStream fo = null;
        File f = createFile(file);
        try {
            if (f != null) {
                fo = new FileOutputStream(f);
                if (encoding == null) {
                    fo.write(text.getBytes());
                } else {
                    fo.write(new String(text.getBytes(), encoding).getBytes());
                }
                fo.flush();
                fo.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            rtn = 1;
        } finally {
            try {
                if (fo != null)
                    fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return rtn;
    }

    public static int createFile(String file, String text) {
        return createFile(file, text, null);
    }

    public static int appendFile(String file, String text) {
        return appendFile(file, text, null);
    }

    public static int appendFile(String file, String text, String encoding) {
        int rtn = 0;
        try {
            FileWriter fw = new FileWriter(file, true);
            if (fw != null) {
                BufferedWriter bw = new BufferedWriter(fw);
                if (encoding == null) {
                    bw.append(text);
                } else {
                    bw.append(new String(text.getBytes(), encoding));
                }
                bw.flush();
                bw.close();
                fw.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            rtn = 1;
        }
        return rtn;
    }

    public static int makeParentDir(File file) {
        return makeDir(file.getParentFile());
    }

    public static int makeDir(File file) {
        int rtn = 0;
        if (file.exists())
            return 0;
        if (file.isFile())
            return 0;
        if (file.isDirectory())
            return 0;
        if (!file.isDirectory())
            file.mkdirs();
        return rtn;
    }
}

Related

  1. writeFile(List fileLines, String pathname)
  2. writeFile(List lines, String filePath)
  3. writeFile(List strList, String fileFullPath)
  4. writeFile(List txtList, String MyFilePath)
  5. writeFile(String file, String content)
  6. writeFile(String filePath, List lines)
  7. writefile(String path, String content)
  8. writefile(String path, String content)
  9. writeFile(String path, String content)