我有以下檔案夾結構
.
└── sample/
├── A-Star/
│ ├── bonnet.png
│ ├── center of front bumper.png
│ └── center of rear bumper.png
├── Alto/
│ ├── bonnet.png
│ ├── center of front bumper.png
│ └── center of rear bumper.png
└── Suzuki/
├── bonnet.png
├── center of front bumper.png
└── center of rear bumper.png
我想一個一個回圈目錄,然后將檔案名轉換為大寫,對于單個單詞bonnet.png應該是BONNET.png,但是對于有空格的檔案名 center of front bumper,它應該是CENTER_OF_FRONT_BUMPER.png。
輸出應如下所示
.
└── sample/
├── A-Star/
│ ├── bonnet.png
│ ├── center of front bumper.png
│ └── center of rear bumper.png
├── Alto/
│ ├── bonnet.png
│ ├── center of front bumper.png
│ └── center of rear bumper.png
├── Suzuki/
│ ├── bonnet.png
│ ├── center of front bumper.png
│ └── center of rear bumper.png
└── output/
├── A-Star/
│ ├── BONNET.png
│ ├── CENTER_OF_FRONT_BUMPER.png
│ └── CENTER_OF_REAR_BUMPER.png
├── Alto/
│ ├── BONNET.png
│ ├── CENTER_OF_FRONT_BUMPER.png
│ └── CENTER_OF_REAR_BUMPER.png
└── Suzuki/
├── BONNET.png
├── CENTER_OF_FRONT_BUMPER.png
└── CENTER_OF_REAR_BUMPER.png
我寫了下面的代碼,但它沒有按預期作業。
首先,它能夠將檔案名設為大寫(不附加下劃線),但它不會創建父目錄,并且在不創建父目錄的情況下它會復制其中的檔案。
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class Test {
public void listFiles(String startDir) throws IOException {
File dir = new File(startDir);
File[] files = dir.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
// Check if the file is a directory
if (file.isDirectory()) {
// We will not print the directory name, just use it as a new
// starting point to list files from
listFiles(file.getAbsolutePath());
// Path source = file.toPath();
// Files.move(source, source.resolveSibling(file.getName().toUpperCase()));
} else {
String path = "D:" File.separator "sample" File.separator "After Rename";
// Use relative path for Unix systems
File f = new File(path);
f.getParentFile().mkdirs();
f.createNewFile();
// We can use .length() to get the file size
// String DestinationPath="D:\\sample\\After Rename";
Path source = file.toPath();
File oldFile = new File(source.toString());
file.getParentFile().mkdirs();
file.createNewFile();
file.getName().toUpperCase();
// File newFile = new File(path,file);
//// Files.move(source, source.resolveSibling(file.getName().toUpperCase()));
// oldFile.renameTo(newFile);
System.out.println("" oldFile.getName());
// System.out.println(file.getName() " (size in bytes: " file.length() ")");
}
}
}
}
public static void main(String[] args) throws IOException {
Test test = new Test();
String startDir = ("D:\\sample");
test.listFiles(startDir);
}
}
uj5u.com熱心網友回復:
使用File和遞回File.listFiles()使邏輯更難遵循。考慮切換到Files.find哪個可以產生少量代碼的目錄掃描:
public static void main(String[] args) throws IOException {
Path src = Path.of(args[0]);
Path dst = Path.of(args[1]);
try (Stream<Path> stream = Files.find(src, Integer.MAX_VALUE, (path, attr) -> attr.isRegularFile())) {
stream.forEach(System.out::println);
}
}
以上只是列印要處理的檔案的名稱。然后,您可以將操作交換為執行重命名的操作,并且可以調整以在復制之前應用構建目錄的操作:
try (Stream<Path> stream = Files.find(src, Integer.MAX_VALUE, (path, attr) -> attr.isRegularFile())) {
stream.forEach(file -> {
// Work out new file name converting basename to uppercase:
String fn = file.getFileName().toString();
int dot = fn.lastIndexOf('.');
if (dot > 0)
fn = fn.substring(0,dot).toUpperCase().replace(' ', '_') fn.substring(dot);
// Work out where to copy the new file under the same path of the dst directory:
Path target = dst.resolve(src.relativize(file)).resolveSibling(fn);
System.out.println("Files.createDirectories(" target.getParent() ");");
System.out.println("Files.copy(" file "," target ");");
/**
// TODO:
try
{
Files.createDirectories(target.getParent());
Files.copy(file,target);
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
*/
});
}
在您掃描的目錄中生成檔案絕不是一個好主意 - 因為這意味著您無法在相同的輸入上重新運??行,因為您將加倍檔案數量。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/511794.html
標籤:爪哇文件
