通过FileSystemView.getFile(parent,false)获取的文件对于isDirectory()和isFile()都返回true

时间:2017-07-31 14:16:10

标签: java windows file

我正在使用像Windows文件浏览器这样的文件搜索功能来编写Java桌面应用程序。对于这个应用程序,我使用了来自javax.swing.filechooser。*的类FileSystemView。 经过一些测试后,我注意到File对象的方法是FileSystemView.getFiles(parent,false);对于类File的isFile()和isDirectory()方法都返回true。

此文件是指向C:\

下另一个文件夹的Windows链接

相反,一个文件(总是一个Windows链接)使用相同的FileSystemView方法,但与.exe文件相关,返回正常值。

我的桌面上有所有这些链接,这里有一个测试例程: (只需将链接放到文件夹和桌面文件中)

import java.io.File;
import javax.swing.filechooser.FileSystemView;

public class Test {
    public static void main(String a[]) {
        //get the FileSystemView instance and root files
        FileSystemView fsv = FileSystemView.getFileSystemView();
        File[] rootList = fsv.getRoots();

        for(File root : rootList) {
            System.out.println("Root file: "+root.getName() +"       ("+root.getAbsolutePath()+")");
        //scan root childrens 
        File[] childrens = fsv.getFiles(root, true);
        if(childrens != null) {
            for(File atm : childrens) {
                System.out.println("\tScanning  "+root.getName()+"\\"+atm.getName());
                System.out.println("\t\t> isFile: "+atm.isFile()+"\tisDirectory: "+atm.isDirectory());
            }
        }
    }

    //this link (on my desktop) is pointing to a folder (under C:\\COLLAUDI)
    File windowsShortcutToFolder = new File("C:\\Documents and Settings\\SIC\\Desktop\\COLLAUDI.lnk");

    //this link (on my desktop) is pointing to a file (under C:\\GestApp\\GeInt\\GeInt.exe)
    File windowsShortcutToFile = new File("C:\\Documents and Settings\\SIC\\Desktop\\GeInt.lnk");

    //Here isFile and isDirectory return with the right values 
    System.out.println("\nScanning files properties: ");
    System.out.println("Scanning "+windowsShortcutToFolder);
    System.out.println("\t> isDirectory:"+windowsShortcutToFolder.isDirectory()
        +"\t> isFile:"+windowsShortcutToFolder.isFile());
    System.out.println("Scanning "+windowsShortcutToFile);
    System.out.println("\t> isDirectory:"+windowsShortcutToFile.isDirectory()
        +"\t> isFile:"+windowsShortcutToFile.isFile());
    }
}

这是输出:

Root file: Desktop (C:\Documents and Settings\SIC\Desktop)
Scanning Desktop\::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
    > isFile: false isDirectory: true
Scanning Desktop\::{208D2C60-3AEA-1069-A2D7-08002B30309D}
    > isFile: false isDirectory: true
Scanning Desktop\Documenti
    > isFile: false isDirectory: true
Scanning Desktop\COLLAUDI.lnk
    > isFile: true  isDirectory: true
Scanning Desktop\GeInt.lnk
    > isFile: true  isDirectory: false

Scanning files properties: 
Scanning C:\Documents and Settings\SIC\Desktop\COLLAUDI.lnk
    > isDirectory:false > isFile:true
Scanning C:\Documents and Settings\SIC\Desktop\GeInt.lnk
    > isDirectory:false > isFile:true

为什么我有这个价值观?

0 个答案:

没有答案