Java基础知识—File文件操作练习作业—04

这篇博客介绍如何在Java中通过File类完成文件夹的拷贝,包括使用file.list()和file.listFiles()两种方法,涉及源文件夹和目标文件夹的键盘录入操作。
摘要由CSDN通过智能技术生成

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值