如何优化多文件复制代码?当前两次调用Files.copy可正常运行
Great question! Duplicating Files.copy calls works, but it's definitely cleaner to avoid repetition—here are a couple of solid approaches depending on your exact needs:
1. Batch Copy Specific Files (Follow the DRY Principle)
If you only need to copy these two specific files (and might add more later), you can bundle their source/target path pairs into a collection and loop through them. This eliminates redundant code and makes future updates simpler:
import java.nio.file.Files import java.nio.file.Paths import java.nio.file.StandardCopyOption fun main() { // Define pairs of (source file path, target file path) val fileCopyTasks = listOf( "src/main/resources/corpusDirectory/corpus.mallet" to "src/main/resources/corpus.mallet", "src/main/resources/corpusDirectory/inferencer" to "src/main/resources/inferencer" ) // Process each copy task in a loop fileCopyTasks.forEach { (sourcePath, targetPath) -> Files.copy( Paths.get(sourcePath), Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING ) } }
This way, if you need to copy additional files later, you just add a new pair to the fileCopyTasks list instead of writing another full Files.copy block.
2. Recursively Copy Entire Directory (For Full Folder Sync)
If your goal is to copy all files and subdirectories from corpusDirectory to the target location (not just the two specific files), use Files.walkFileTree to recursively traverse the source directory and replicate its structure in the target:
import java.nio.file.Files import java.nio.file.Paths import java.nio.file.StandardCopyOption import java.nio.file.FileVisitResult import java.nio.file.SimpleFileVisitor import java.nio.file.attribute.BasicFileAttributes fun copyDirectory(sourceDir: String, targetDir: String) { val sourcePath = Paths.get(sourceDir) val targetPath = Paths.get(targetDir) // Create target directory (and parent directories if needed) Files.createDirectories(targetPath) Files.walkFileTree(sourcePath, object : SimpleFileVisitor<Path>() { // Copy individual files override fun visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult { val targetFile = targetPath.resolve(sourcePath.relativize(file)) Files.copy(file, targetFile, StandardCopyOption.REPLACE_EXISTING) return FileVisitResult.CONTINUE } // Recreate subdirectory structure in target override fun preVisitDirectory(dir: Path, attrs: BasicFileAttributes): FileVisitResult { val targetSubDir = targetPath.resolve(sourcePath.relativize(dir)) Files.createDirectories(targetSubDir) return FileVisitResult.CONTINUE } }) } // Usage: Copy everything from corpusDirectory to the resources root copyDirectory("src/main/resources/corpusDirectory", "src/main/resources/")
This approach is ideal if you want to keep the target directory in sync with the source, even as files are added or removed from corpusDirectory.
Quick Decision Guide
- Use the batch pair list if you only need to copy a fixed, known set of files. It’s simple and focused.
- Use the recursive directory copy if you need to replicate an entire folder structure (including future changes to the source directory).
内容的提问来源于stack exchange,提问作者danD

