3 간단한 애플릿은 파일을 복사하는 자바 코드를 사용하여, Ctrl + C 및 Ctrl + V 복사 파일을

하지 Ctrl + C 및 Ctrl + V 파일을 복사하는 자바 코드를 사용하여 파일을 복사합니다

public class Test4 {
	
	public static void main(String[] args) {
		System.out.println("输入文件路径:");
		String s1 = new Scanner(System.in).nextLine();
		File from = new File(s1);
		if(!from.isFile()) {
			System.out.println("请输入正确的文件");
			return;
		}
		System.out.println("请输入目标文件路径:");
		String s2 = new Scanner(System.in).nextLine();
		File to = new File(s2);
		if(to.isDirectory()) {
			System.out.println("请输入正确的目标文件路径:");
			return;
		}
		try {
			copy(from, to);
			System.out.println("复制完成");
		} catch (Exception e) {
			System.out.println("复制失败");
			e.printStackTrace();
		}
	}
	private static void copy(File from, File to) throws Exception {
		FileInputStream in = new FileInputStream(from);
		FileOutputStream out = new FileOutputStream(to);
		int b = 0;
		while((b = in.read()) != -1) {
			out.write(b);
		}
		in.close();
		out.close();
	}
}

콘솔:
그림 삽입 설명 여기

출시 구 개 원래 기사 · 원 찬양 2 · 조회수 274

추천

출처blog.csdn.net/weixin_44941564/article/details/104534204