Here you can find the source of writeStringToFile(String contents, String path, String encoding)
Parameter | Description |
---|---|
contents | The string to write |
path | The file path |
encoding | The encoding to encode in |
Parameter | Description |
---|---|
IOException | In case of failure |
public static void writeStringToFile(String contents, String path, String encoding) throws IOException
//package com.java2s; import java.io.*; import java.util.zip.GZIPOutputStream; public class Main { /**//w w w . ja v a 2 s . c o m * Writes a string to a file. * * @param contents The string to write * @param path The file path * @param encoding The encoding to encode in * @throws IOException In case of failure */ public static void writeStringToFile(String contents, String path, String encoding) throws IOException { OutputStream writer = getBufferedOutputStream(path); writer.write(contents.getBytes(encoding)); writer.close(); } private static OutputStream getBufferedOutputStream(String path) throws IOException { OutputStream os = new BufferedOutputStream(new FileOutputStream( path)); if (path.endsWith(".gz")) { os = new GZIPOutputStream(os); } return os; } }