Java 一次性读取或写入文件内容

时间:2014-12-17 16:22:31   收藏:0   阅读:118

Java 一次性读取或写入文件内容

public class IOHelper {

	public static void copy(Reader in,Writer out) throws IOException {
		int c = -1;
		while((c = in.read()) != -1) {
			out.write(c);
		}
	}
	
	public static String readFile(File file) throws IOException {
		if (file != null && file.canRead()){
			Reader in = new FileReader(file);
			StringWriter out = new StringWriter();
			copy(in,out);
			return out.toString();
		}
		return "";
	}
	
	public static void saveFile(File file,String content) throws IOException {
		Writer writer = new FileWriter(file);
		writer.write(content);
		writer.close();
	}
	
}


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!