lyl890

导航

文件的读写

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

  

posted on 2022-07-25 22:54  静静书中仙  阅读(12)  评论(0编辑  收藏  举报