Here you can find the source of writeFile(File f, String contents)
Parameter | Description |
---|---|
f | the file to be written |
contents | the contents to be written |
Parameter | Description |
---|---|
IOException | an exception |
public static void writeFile(File f, String contents) throws IOException
//package com.java2s; /* MonkeyTalk - a cross-platform functional testing tool Copyright (C) 2012 Gorilla Logic, Inc.//from w ww. j av a 2 s. c o m This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Writer; public class Main { /** * Write the given contents with UTF-8 encoding to the given file. * * @param f * the file to be written * @param contents * the contents to be written * @throws IOException */ public static void writeFile(File f, String contents) throws IOException { Writer out = new OutputStreamWriter(new FileOutputStream(f), "UTF-8"); try { out.write(contents); } finally { out.close(); } } /** * Write the given contents with UTF-8 encoding to the given filename. * * @param filename * the filename * @param contents * the contents to be written * @throws IOException */ public static void writeFile(String filename, String contents) throws IOException { writeFile(new File(filename), contents); } /** * Write the given raw bytes to the given file. * * @param f * the file to be written * @param bytes * the raw bytes to be written * @throws IOException */ public static void writeFile(File f, byte[] bytes) throws IOException { OutputStream out = new FileOutputStream(f); try { out.write(bytes); } finally { out.close(); } } /** * Write the binary input stream to the given file. * * @param f * the file to be written * @param in * the contents * @throws IOException */ public static void writeFile(File f, InputStream in) throws IOException { byte[] buf = new byte[4096]; try { OutputStream out = new FileOutputStream(f); try { int len; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } } finally { out.close(); } } finally { in.close(); } } }