0

I have written a short program that will find a file I have made and print some of its details. It executes all right, but it cannot detect the file size or if it is hidden or not. E.G.

file path: C:\temp\filetext.txt last modified: 0 file size: 0 Is file hidden?false

The file does exist in the temp folder on C. I'm not really sure what the problem is

public void Q1()
{
    String fileName = "filetext.txt";
    getFileDetails(fileName);
}

public void getFileDetails(String fileName)
{
    String dirName = "C:/temp/";
    File productsFile = new File(dirName + fileName);
    long size = productsFile.length();
    System.out.println("file path: " + productsFile.getAbsolutePath() + " last modified: " + productsFile.lastModified() + " file size: " + productsFile.length() + " Is file hidden?" + productsFile.isHidden());
}
4

1 回答 1

2

文件不需要使用物理文件。因此,即使它应该代表的物理文件不存在/找不到,您的 File 对象也可以存在。检查 JavaDoc 中的length()和,如果文件不存在lastModified(),它们都会返回。因此,通过在调用其他方法之前调用,0L确保您的 File 对象链接到文件系统上的现有文件。file.exists()

于 2013-05-01T17:14:51.277 回答