Java FileOutputStream Create writeFileByLine(String content, String filename, boolean append)

Here you can find the source of writeFileByLine(String content, String filename, boolean append)

Description

write File By Line

License

Apache License

Declaration

public static void writeFileByLine(String content, String filename, boolean append) 

Method Source Code

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

import java.io.File;

import java.io.FileOutputStream;

import java.io.PrintWriter;

public class Main {

    public static void writeFileByLine(String content, String filename, boolean append) {
        File file = new File(filename);
        String pathname = new File(filename).getParent();
        if (!new File(pathname).exists()) {
            new File(pathname).mkdirs();
        }/*from   w w w. ja  v a 2 s .com*/
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new FileOutputStream(file, append));
            writer.print(content);
            writer.println();
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    public static void writeFileByLine(String content, String filename) {
        File file = new File(filename);
        PrintWriter writer = null;
        try {
            writer = new PrintWriter(new FileOutputStream(file));
            writer.print(content);
            writer.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

Related

  1. writeFileAsString(final File filePath, final String output, final String charset)
  2. writeFileAsString(String filename, String contents)
  3. writeFileBinary(String filename, byte[]... dataArrays)
  4. writeFileByBytes(String content, String filename)
  5. writeFileByChar(String content, String filename, String encoding)
  6. writeFileBytes(File pFile, byte[] pBytes)
  7. writeFileContent(String filepath, byte[] content)
  8. writeFileContent(String sFileName, byte[] content)
  9. writeFileCover(String path, String content)