java删除文件夹包括子目录_java基础 File 递归删除文件夹中所有文件文件夹 目录(包含子目录)下的.java文件复制到e:/abc文件夹中, 并统计java文件的个数...

packagecom.swift.test;importjava.io.File;importjava.io.FileFilter;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.Scanner;/*从控制台获取输入的文件目录然后将该目录(包含子目录)下的.java文件复制到e:/abc文件夹中,

并统计java文件的个数.

提示:如果有相同的名称的文件,如果两个Test01.java,则拷贝到目标文件夹时只能有一个Test01.java,

另一个Test01.java要修改为另一个名称:该名称可随机生成,只要不重复即可.*/

public classCopyJavaFileCount {private static int sum=0;public static void main(String[] args) throwsIOException {

Scanner scan=newScanner(System.in);

String path=scan.nextLine();

File src=newFile(path);

File dest=new File("d:/asdf");if(!dest.exists()) {

dest.mkdirs();

}

copy(src,dest);

System.out.println(".java文件的个数是"+sum);

}private static void copy(File src, File dest) throwsIOException {

File[] files= src.listFiles(newFileFilter() {

@Overridepublic booleanaccept(File pathname) {if(pathname.isDirectory()) {try{

copy(pathname,dest);

}catch(IOException e) {

e.printStackTrace();

}

}if(pathname.getName().toLowerCase().endsWith(".java")) {return true;

}return false;

}

});for(File file:files) {

String name=file.getName();

File destFile=newFile(dest,name);

copyFile(file,destFile);

System.out.println(file);

}

sum=sum+files.length;

}public static void copyFile(File file1,File file2) throwsIOException{

FileInputStream fis=newFileInputStream(file1);//2.创建字节输出流对象关联文件路径:D盘下的a.png

FileOutputStream fos=newFileOutputStream(file2);//3.定义一个字节数组,用来存放读取到文件字节数

byte[] buf=new byte[128];intlen;//4.使用循环不断从字节输入流读取一个字节数组,每读取一个字节数组就利用输出流写出一个字节数组。

while((len=fis.read(buf))!=-1) {

fos.write(buf,0,len);

}

System.out.println("已经copy成功");//5.关闭流,释放资源

fis.close();

fos.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Apache Commons Net 库的 FTPClient 类来实现 Java FTP 递归下载文件的功能。下面是一个示例代码,可以下载 FTP 服务器上指定目录下的所有文件及其子目录下的所有文件文件夹: ```java import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPFile; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FtpDownloader { public static void main(String[] args) { String server = "ftp.example.com"; int port = 21; String username = "username"; String password = "password"; String remoteDirPath = "/path/to/remote/directory"; String localDirPath = "/path/to/local/directory"; FTPClient ftpClient = new FTPClient(); try { ftpClient.connect(server, port); ftpClient.login(username, password); ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); downloadDirectory(ftpClient, remoteDirPath, localDirPath); ftpClient.logout(); } catch (IOException e) { e.printStackTrace(); } finally { if (ftpClient.isConnected()) { try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } } private static void downloadDirectory(FTPClient ftpClient, String remoteDirPath, String localDirPath) throws IOException { FTPFile[] subFiles = ftpClient.listFiles(remoteDirPath); if (subFiles != null && subFiles.length > 0) { for (FTPFile ftpFile : subFiles) { String remoteFilePath = remoteDirPath + "/" + ftpFile.getName(); String localFilePath = localDirPath + "/" + ftpFile.getName(); if (ftpFile.isDirectory()) { // create the directory in local file system File localDir = new File(localFilePath); if (!localDir.exists()) { localDir.mkdir(); } // download the sub directory recursively downloadDirectory(ftpClient, remoteFilePath, localFilePath); } else { // download the file OutputStream outputStream = new FileOutputStream(localFilePath); ftpClient.retrieveFile(remoteFilePath, outputStream); outputStream.close(); } } } } } ``` 在上面的代码,`downloadDirectory()` 方法递归地下载指定目录下的所有文件文件夹。如果遇到子目录,它会在本地文件系统创建一个对应的目录,并递归下载子目录下的所有文件文件夹。如果遇到普通文件,它会将其下载到本地文件系统。 你需要将代码的 `server`,`port`,`username`,`password`,`remoteDirPath` 和 `localDirPath` 替换为你自己的 FTP 服务器地址、端口、用户名、密码、远程目录和本地目录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值