Java Text File Write writeString(String filePath, String content)

Here you can find the source of writeString(String filePath, String content)

Description

Write a string into a file

License

Open Source License

Parameter

Parameter Description
filePath : the name of file to be written
content : the content of a string to be written

Exception

Parameter Description
Exceptionif error occurs

Declaration

public static void writeString(String filePath, String content) throws Exception 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.io.*;

public class Main {
    /**//from  www .  j  a v a2  s.c om
     * Write a string into a file
     *
     * @param filePath : the name of file to be written
     * @param content  : the content of a string to be written
     * @throws Exception  if error occurs
     */
    public static void writeString(String filePath, String content) throws Exception {
        writeString(filePath, content, false);
    }

    /**
     * Write a string into a file with the given path and content.
     *
     * @param filePath path of the file
     * @param content  content to write
     * @param append   whether append
     * @throws Exception if error occurs
     */
    public static void writeString(String filePath, String content, boolean append) throws Exception {
        String dirPath = filePath.substring(0, filePath.lastIndexOf("/") + 1);
        File dir = new File(dirPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        BufferedWriter bw = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(filePath, append), "UTF-8"));
        if (content.endsWith("\n"))
            bw.write(content);
        else
            bw.write(content + "\n");
        bw.close();
    }
}

Related

  1. writeString(String contents, File file)
  2. writeString(String data, File file)
  3. writeString(String data, String file_path)
  4. writeString(String fileName, String str)
  5. writeString(String filePath, boolean append, String content, String encoding)
  6. writeString(String par0)
  7. writeString(String path, String key, String value)
  8. writeString(String path, String str)
  9. writeString(String path, String str, boolean append)