Here you can find the source of writeStringToFile(String path, String content)
Parameter | Description |
---|---|
path | Path of the file to be written. |
content | Content of the file. |
Parameter | Description |
---|---|
FileNotFoundException | an exception |
IOException | an exception |
IllegalArgumentException | an exception |
public static void writeStringToFile(String path, String content) throws IOException, IllegalArgumentException
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { /**/*from www. j a v a 2 s. c om*/ * Write the given string content into a file with the specified path. * * @param path Path of the file to be written. * @param content Content of the file. * @throws FileNotFoundException * @throws IOException * @throws IllegalArgumentException */ public static void writeStringToFile(String path, String content) throws IOException, IllegalArgumentException { writeStringToFile(new File(path), content); } /** * Write the given string content into the given {@link File}. * * @param file File to write. * @param content Content of the file. * @throws FileNotFoundException * @throws IOException * @throws IllegalArgumentException */ public static void writeStringToFile(File file, String content) throws IOException, IllegalArgumentException { if (file == null) { throw new IllegalArgumentException("File should not be null."); } if (!file.exists()) { throw new FileNotFoundException("File does not exist: " + file); } if (!file.isFile()) { throw new IllegalArgumentException("Should not be a directory: " + file); } if (!file.canWrite()) { throw new IllegalArgumentException("File cannot be written: " + file); } try (Writer output = new BufferedWriter(new FileWriter(file))) { //FileWriter always assumes default encoding is OK! output.write(content); } } }