Java Realizes the Super concise Writing Method of Copying Files and Naming

  • 2021-12-12 04:33:32
  • OfStack

The super concise writing method of copying files and naming directories is good. The code file is renamed and copied. 1 new file transmission parameter description

Ultra-concise writing of copying files and naming them

Yes, it's me again. This time, I bring you the ultra-concise writing of copying files and naming in Java (please make sure your jre is 1.8 +). This time, I use Files (starting from 1.7) and lambda expressions (starting from 1.8), which are relatively new things, and there are also some exciting features (and love).

All right, put on the code


DirectoryStream<Path> directoryStream;
File in = new File("C:\\Users\\simon\\Desktop\\a"); //  Resource folder 
File out = new File("C:\\Users\\simon\\Desktop\\b"); //  Destination folder 
try {
       directoryStream = Files.newDirectoryStream(in.toPath()); //returning a DirectoryStream to iterate over* all entries in the directory.
       directoryStream.forEach(path -> {
           if (path.getFileName().toString().endsWith(".java")) { //  Determine whether it is java Documents 
               try {
                   Files.copy(path, out.toPath().resolve(path.getFileName().toString().replace(".java", ".txt")), StandardCopyOption.REPLACE_EXISTING); //  Rename to .txt  And copy to out Folder 
               } catch (IOException e) { //  Because in lambda Expression, so you want to wrap try catch
                   e.printStackTrace();
               }
           }
       });
   } catch (IOException e) {
       e.printStackTrace();
   }

File rename copy 1 new file

java file renaming and keeping the old file is actually copying a new file, which is equivalent to copying and pasting renaming. The code is as follows:

Transmission parameter description

The old file addresses, oneType and twoType and count are my own business and renamed names of files. The collection of files is for my convenience in exporting and compressing these files. See this article


 // Rename the picture 
    public String reNameImg(String oldPath, String oneType, String twoType, int count, List<File> files) {
        File source = new File(oldPath);
        String suffixName = source.getName().substring(source.getName().lastIndexOf("."));
        File filePath = new File(rootFileDir + File.separator + "exPort" + File.separator +generatorDataFileName());
        String reName = "";
        try {
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
           File dest =new File(filePath+File.separator+oneType + "-" + twoType + "-" + count + suffixName);
            Files.copy(source.toPath(), dest.toPath(), StandardCopyOption.REPLACE_EXISTING);
            files.add(dest);
            reName = dest.getPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reName;
    }

 // Get the date directory 
    private String generatorDataFileName() {
        Calendar date = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date.getTime());
    }

Related articles: