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

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();
	}
	
}


你可能感兴趣的:(java,String,文件,读取,写入)