我正在运行一个程序来列出文件夹中存储的所有文件的信息。
我想获取文件的属性(对我来说最重要的是文件大小,但我还想获取其他属性,例如修改日期等)。
我的问题是,当我访问另一个程序实际使用的文件时,我无法获取文件的 BasicFileAtrributtes
。我尝试使用 File
、URL
、RandomFileAcces
,但所有这些都需要打开文件,并抛出异常,例如:
java.io.FileNotFoundException:C:\pagefile.sys(访问被拒绝)
java中是否有任何选项可以获取此属性?我不喜欢使用任何额外的库,以保持应用程序的小尺寸。
应用程序基于java JRE7。
我正在使用java.nio.file.SimpleFileVisitor
来访问所有文件。
这是我的代码片段,其中出现错误:
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc){
FileInfo temp=new FileInfo(new FileInfo().repairName(file.toString()));
temp.isLeaf=true;
temp.fName=temp.fName.replace(strIn, "");
File fis=null;
try {
fis=new File(file.toAbsolutePath().toString());
if(fis.exists())
System.out.println("exists");
if(fis.isFile())
System.out.println("isFile");
System.out.println(file.toAbsolutePath().toString());
temp.fSize=new BigInteger(new Long(fis.length()).toString());
} catch(Exception e){
e.printStackTrace();
}
node.add(temp, true);
FileListCreator.jProgressBar.setValue(++count);
return CONTINUE;
}
最佳答案
这对我来说效果很好:
File temp = new File("c:\\pagefile.sys");
System.err.println(temp.length());
System.err.println(temp.lastModified());
关于java - 访问被拒绝 - 获取文件属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10025482/