1 Star 0 Fork 0

GGBondlctrl/IO

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
IODemo9.java 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
GGBondlctrl 提交于 2024-10-14 23:15 +08:00 . csdnIO操作实例代码
package io;
import java.io.*;
import java.util.Scanner;
//文件的复制
public class IODemo9 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入你要复制的文件");
String oldPath=sc.next();
System.out.println("请输入要复制的目标文件");
String newPath=sc.next();//必须有一个目标目录
//这里进行要复制文件的存在判断
File file=new File(oldPath);
if(!file.isFile()){
System.out.println("这里你输入的文件有误");
return;
}
File file1=new File(newPath);
if (!file1.getParentFile().isDirectory()){
System.out.println("这里的目标文件复制地址有误");
return;
}
//进行复制的操作
try (InputStream input=new FileInputStream(file);
OutputStream output=new FileOutputStream(file1)){
//进行读取字节
byte[] buffer;
while (true){
buffer=new byte[1024];
int n=input.read(buffer);
if (n==-1){
break;
}
output.write(buffer);
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/GGBondlctrl/io.git
git@gitee.com:GGBondlctrl/io.git
GGBondlctrl
io
IO
master

搜索帮助