传统IO实现文件的拷贝

来源:互联网 发布:网络伤感歌曲 编辑:程序博客网 时间:2024/06/08 19:07
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class CopyAndPaste {public static void main(String[] args) {String filename = "c://hello.txt";String toFilename = "d://world.txt";try {FileInputStream is = new FileInputStream(filename);FileOutputStream os = new FileOutputStream(toFilename);byte[] buffer = new byte[1024 * 1024];while (is.read() != -1) {int ins = is.read(buffer);// ins==-1 表示已经读取完文件内容。if (ins == -1) {is.close();// 关闭文件输入流os.flush();// 将缓冲区的内容强制输出。以免造成数据流失os.close();// 关闭文件输出流} else {// System.out.println(is.read());os.write(buffer, 0, ins);// 将文件内容输出}}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

原创粉丝点击