在当前目录下复制所有子项并在他们名字后面加上_cp,文件夹直接添加,文件名需要在格式前添加,报错

 File file = new File(".");
        int d;
        if(file.isDirectory()){
            File[] subs = file.listFiles();
            for (File sub : subs) {
                String name = sub.getName();
                FileInputStream fis = new FileInputStream(name);
               if(sub.isFile()){
                   StringBuilder ni=new  StringBuilder(name);
                    int index = ni.indexOf(".");
                   ni.insert(index,"_cp");
                   name=String.valueOf(ni);

               }else{
                    name+="_cp";
               }
               File file1=new File(name);
              FileOutputStream fos = new FileOutputStream(name);
              while((d=fis.read())!=-1){
                  fos.write(d);
              }
              fis.close();
              fos.close();
            }
            System.out.println("复制完毕!");


img

参考一下:

import java.io.*;

public class CopyAllFiles {
    public static void main(String[] args) {
        File folder = new File(".");
        String[] fileList = folder.list();
        
        for (String fileName : fileList) {
            try {
                File file = new File(fileName);
                if (file.isDirectory()) {
                    File newDir = new File(file.getName() + "_cp");
                    newDir.mkdir();
                } else {
                    String newName = file.getName();
                    int dotIndex = newName.lastIndexOf(".");
                    if (dotIndex > 0) {
                        newName = newName.substring(0, dotIndex) + "_cp" + newName.substring(dotIndex);
                    } else {
                        newName = newName + "_cp";
                    }
                    File newFile = new File(newName);
                    InputStream in = new FileInputStream(file);
                    OutputStream out = new FileOutputStream(newFile);
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    in.close();
                    out.close();
                }
            } catch (IOException e) {
                System.out.println("Error processing file " + fileName);
                e.printStackTrace();
            }
        }
    }
}


  • 帮你找了个相似的问题, 你可以看下: https://ask.csdn.net/questions/219807
  • 除此之外, 这篇博客: 测试、测试开发面试准备和复习中的 cp:复制文件的和目录 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 命令语法:cp [选项] [源文件|目录] [目标文件|目录]

    选项选项含义
    -a在复制目录时保留链接、文件属性、并递归地复制目录,等同于-dpr选项
    -d复制时保留链接
    -f在覆盖目标文件之前不给出提示信息要求用户确认
    -i和-f选项相反看,在覆盖目标文件之前给出提示信息,要求用户确认
    -p出复制源文件的内容外,还把其修改时间和访问权限也复制到新文件中
    -l不做复制,只是链接文件
    -r如果给出的源文件是一个目录文件,将递归复制该目录下所有的子目录和文件。此时目标必须为一个目录名

    例子:将/etc/grub2.cfg文件复制到/root目录下,并改名为grub

    [root@localhost ~]# cp /etc/grub2.cfg /root/grub
    cp: overwrite ‘/root/grub’? y
    [root@localhost ~]# ls
    anaconda-ks.cfg  grub  newdir1
    

    将/etc/grub2.cfg文件复制到/root目录下

    [root@localhost ~]# cp /etc/grub2.cfg /root
    [root@localhost ~]# ls
    anaconda-ks.cfg  grub  grub2.cfg  newdir1
    [root@localhost ~]# 
    

    将/boot目录以及该目录中的所有文件和子目录都复制到/root目录中

    [root@localhost ~]# cp -r /boot /root
    [root@localhost ~]# ls -l /root
    total 24
    -rw-------. 1 root root 1260 Jun  9 14:21 anaconda-ks.cfg
    dr-xr-xr-x. 5 root root 4096 Jun 22 23:21 boot
    -rw-r--r--. 1 root root 4287 Jun 22 23:15 grub
    -rw-r--r--. 1 root root 4287 Jun 22 23:18 grub2.cfg
    drwxr-xr-x. 2 root root    6 Jun 22 22:27 newdir1