您当前的位置: 首页 >  Java

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

发布时间:2014-12-17 16:00:30 ,浏览量:0

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();
	}
	
}
关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    105936博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.4478s