其他分享
首页 > 其他分享> > 文件的读写

文件的读写

作者:互联网

public void copyFile(String source, String dest) throws Exception {
// 构建文件输入出流。
InputStream is = new FileInputStream(new File(source));
OutputStream os = new FileOutputStream(new File(dest));
// 构建 InputStreamReader, OutputStreamWriter
Reader reader = new InputStreamReader(is, "UTF-8");
Writer writer = new OutputStreamWriter(os, "UTF-8");
// 字符缓冲区。
char[] buffer = new char[1024];
int len = 0;
// 读取数据。
while((len = reader.read(buffer)) != -1) {
// 写入数据
writer.write(buffer, 0, len);
}
// 刷新缓冲
writer.flush();
// 关闭资源
reader.close();
writer.close();
is.close();
os.close();
}

  

标签:文件,buffer,读写,writer,new,len,reader,close
来源: https://www.cnblogs.com/lyl890/p/16519195.html