Q
比較文件名
-1
A
回答
-1
這將有助於你得到文件兩條路徑:
import java.io.File;
import java.util.*;
public class ListFiles
{
public static void main(String[] args)
{
// First directory path here.
String path1 = ".";
// Second directory path here.
String path2 = ".";
// File class is very important.
// If you did a simple Google search
// Then you would have seen this class mentioned.
File folder1 = new File(path1);
File folder2 = new File(path2);
// It gets the list of files for you.
File[] listOfFiles1 = folder1.listFiles();
File[] listOfFiles2 = folder2.listFiles();
// We'll need these to store the file names as Strings.
ArrayList<String> fileNames1 = new ArrayList<String>();
ArrayList<String> fileNames2 = new ArrayList<String>();
// Get file names from first directory.
for (int i = 0; i < listOfFiles1.length; i++)
{
if (listOfFiles1[i].isFile())
{
fileNames1.add(listOfFiles1[i].getName());
}
}
// Get file names from second directory.
for (int i = 0; i < listOfFiles2.length; i++)
{
if (listOfFiles2[i].isFile())
{
fileNames2.add(listOfFiles2[i].getName());
}
}
// Now compare
// Loop through the two array lists and add your own logic.
}
}
您需要添加自己的邏輯來進行比較。 Source
+0
爲什麼downvote? –
-1
我有這樣的代碼,所有的目錄中的文件與一個特定的文件進行比較,以檢查是否該文件已位於存在於目錄中,可以調整這一點,按您的needs.It使用公地io.jar
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.io.FileUtils;
public class CompareFile {
String directory;
File file;
public CompareFile(String directory, File file) {
this.directory = directory;
this.file = file;
}
public boolean doesFileExist() {
boolean indicatorFileExist = true;
List<File> files = null;
try {
files = getFiles();
files = files.stream().filter(fileMatch -> {
try {
if(fileMatch.isFile()){
return FileUtils.contentEquals(fileMatch, file);
}else{
return false;
}
} catch (Exception excep) {
excep.printStackTrace();
return false;
}
}).collect(Collectors.toList());
if(files.isEmpty()){
indicatorFileExist = false;
}
} catch (Exception excep) {
excep.printStackTrace();
} finally {
if (files != null) {
files = null;
}
}
return indicatorFileExist;
}
private List<File> getFiles() {
List<File> fileList = null;
try {
if(directory!=null && directory.trim().length()>0 && file!=null){
File dir = new File(directory);
if(dir.isDirectory() && dir.exists() && dir.canRead()){
fileList = Arrays.asList(dir.listFiles());
}
}
} catch (Exception excep) {
excep.printStackTrace();
}
return fileList;
}
}
相關問題
- 1. Win32文件名比較
- 2. 文件比較
- 3. 文件比較
- 4. 文件比較
- 5. 擊(分)文件名比較失敗
- 6. 將文件名與字符串比較
- 7. 使用相同名稱比較文件
- 8. 比較變量與文件名bash
- 9. 比較文件名與整數
- 10. 如何比較整數和文件名?
- 11. 在PDI中比較文件名
- 12. PowerShell文件名與日期比較
- 13. 與通配符的文件名比較
- 14. 比較的NodeJS Unicode文件名
- 15. 比較一個文件名模式
- 16. SQL作業步驟比較文件名
- 17. SQL列數據比較,以文件名
- 18. BeyondCompare部分文件名比較
- 19. Python的 - 文件名和文件夾名稱比較
- 20. 比較文件名與可選文件擴展名
- 21. 比較文件.class Bejond比較4
- 22. 文件夾/文件比較
- 23. 多文件比較
- 24. xml文件比較
- 25. Powershell比較文件
- 26. Unix文件比較
- 27. Jmeter文件比較
- 28. xml文件比較
- 29. C文件比較
- 30. 比較xml文件
有沒有看過java'File'類,你學到了什麼? – vikingsteve
請發佈迄今爲止您嘗試寫入的Java代碼。 – sdanzig
@MarounMaroun什麼顏色?淡紫色? http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/20000/1000/100/21168/21168.strip.gif –