Here you can find the source of writeString(String filePath, String content)
Parameter | Description |
---|---|
filePath | : the name of file to be written |
content | : the content of a string to be written |
Parameter | Description |
---|---|
Exception | if error occurs |
public static void writeString(String filePath, String content) throws Exception
//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(); } }