filecopy.java
来自「这是一个用java编写类似于notepad文件编辑器」· Java 代码 · 共 59 行
JAVA
59 行
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class FileCopy { public static void copy(String from_name, String to_name) throws IOException { File from_file = new File(from_name); File to_file = new File(to_name); if (!from_file.canRead()) abort("源文件不是可读文件: " + from_name); if (to_file.exists()) { if (!to_file.canWrite()) abort("目标文件不是可写文件:" + to_name); } //这时开始复制文件。 FileInputStream from = null; FileOutputStream to = null; try { from = new FileInputStream(from_file); //创建输入流 to = new FileOutputStream(to_file); //创建输出流 byte[] buffer = new byte[4096]; int bytes_read; //缓存中的字节数目 //将一段字节流读到缓存中,然后将它们写出来 //,循环直到文件末尾(当read()返回-1时)停止 while ((bytes_read = from.read(buffer)) != -1) //读取流直到文件末尾 to.write(buffer, 0, bytes_read); //写入 } //关闭流 finally { if (from != null) try { from.close(); } catch (IOException e) { ; } if (to != null) try { to.close(); } catch (IOException e) { ; } } } private static void abort(String msg) throws IOException { throw new IOException("文件复制: " + msg); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?