Java基础知识—File文件操作练习作业—04
4、 键盘录入一个文件夹路径,作为源文件夹;
键盘录入一个文件夹路径,作为目标文件夹
写代码将源文件夹拷贝到目标文件夹中
文中分别使用 file.list() 和 file.listFiles()给出两种解决方法!
方法一:使用String[] strs = file.list();获取一层的文件或者文件夹名称,(不是全路径)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package io.learn;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
/**
*
* @author steven
*/
public class CopyFolder {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("please input srcfolder path:");
String srcpath = in.nextLine();
System.out.println("please input dirfolder path:");
String dirpath = in.nextLine();
File srcfilepath = new File(srcpath);
if (!srcfilepath.exists()) {
System.out.println("Error! input file srcfolder try once again:");
continue;
}
File dirfilepath = new File(srcpath);
if (!dirfilepath.exists