I'm running a program to list info of all files stored in folder.
I want to obtain properties of a file (the most important for me is file size, but i would like to get also other properties, like date of modification, etc.).
My problem is when I get to the file which is actually used by another program, I can't get BasicFileAtrributtes
of file. I've tried to use File
, URL
, RandomFileAcces
, but all of these requiese to open a file, and throw Exception like:
java.io.FileNotFoundException: C:\pagefile.sys (Access is denied)
Is there any option in java to obtain this properties? I prefer not to use any extra libraries, to keep small size of the application.
App is based on java JRE7.
I'm using java.nio.file.SimpleFileVisitor
to visit all the files.
Here is fragment of my code, where I got the error:
@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;
}
If the method java.io.File.exists() returns false, and the file C:\pagefile.sys
exists in your file system, you specified the incorrect file path then.
The following code works on my machine:
package q10025482;
import java.io.File;
public class TestFile {
public static void main(String[] args) {
String fileName = "C:/System Volume Information";//"C:/pagefile.sys"
File file = new File(fileName);
System.out.println("\nFile " + file.getAbsolutePath() + " info:");
System.out.println("Exists: " + file.exists());
System.out.println("Is file: " + file.isFile());
System.out.println("Is dir: " + file.isDirectory());
System.out.println("Length: " + file.length());
System.out.println();
}
}
Here is the result output:
File C:\System Volume Information info:
Exists: true
Is file: false
Is dir: true
Length: 24576