我需要在目录中搜索并获取其中的所有文件。所以我曾经将文件存储在文件数组中。我的问题是
1) 数组是否包含实际文件或对文件的引用? 2)哪个是最好的选择 File 或 RandomAcessFile ?为什么会这样?
请帮我处理一下我的代码
public File[] getAllFiles(String path) {
File file = new File(path);
if (file.isDirectory() && file.exists()) {
allFiles = file.listFiles();
System.out.println("Files in the directory " + file.getName()
+ " present in the path " + file.getAbsolutePath()
+ " are fetched sucessfully");
printAllFiles(allFiles);
}
return allFiles;
}
public void printAllFiles(File data[]) {
int count = 0;
for (File i : data) {
System.out.println("Index : " + count + " Name : " + i.getName());
count++;
}
}
最佳答案
File
是文件/目录的抽象表示,它可能存在也可能不存在。它不消耗任何资源,因此您可以随心所欲地存储它们。
RandomAccessFile
用于实际文件访问(读取、查找、写入),因此您在这里不需要它。
关于java - Java 中的文件与随机访问文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51985873/