copyfile.java
来自「java应用举例2」· Java 代码 · 共 51 行
JAVA
51 行
import java.io.*;
public class CopyFile {
public static void main(String args[]) throws IOException {
//检查程序参数
if(args.length != 2 ) {
System.out.println("程序参数不正确!!");
} else {
//调用自定义的拷贝函数
try{
Paste(args[0],args[1]);
} catch (Exception e0) {
System.out.println("文件读取错误!!");
}
}
}
public static void Paste(String rls, String test) throws IOException {
//建立源文件与目标文件
File src = new File(rls);
File obj = new File(test);
//确定传入的源文件是否存在
if(src.exists() == false) {
System.out.println("源文件不存在!!");
} else {
//建立文件输入流
FileInputStream in = null;
//建立文件输出流
FileOutputStream out = null;
try {
in = new FileInputStream(src);
out = new FileOutputStream(obj);
//创建保存文件内容的容器
byte buffer[] = new byte[4096];
//建立输入流缓冲区
int read;
//读源文件直到文件末尾
while((read = in.read(buffer)) != -1) {
out.write(buffer,0,read);
}
in.close();
out.close();
} catch(IOException e1) {
System.out.println("文件拷贝失败!!");
}
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?