简体   繁体   English

Linux和Windows中的java.io.File和路径

[英]java.io.File and paths in Linux and Windows

I wrote a little programm, which takes path to a directory from the command prompt and prints all files and catalogs that are placed in this directory. 我写了一个小程序,它从命令提示符处获取目录的路径,并打印出放置在该目录中的所有文件和目录。 But it works fine only for Windows. 但是它仅适用于Windows。 I have something like this: 我有这样的事情:

path = args[0];    
File dir = new File(path);
System.out.println(dir.listFiles());

Launch at Windows (works fine): 在Windows上启动(正常运行):

java MyProg C:\mydir

Launch at Linux: 在Linux上启动:

java MyProg /home/user/mydir

And instead of a list of files I get this: 我得到的不是文件列表:

[Ljava.io.File;@190690e

What am I doing wrong and where my cross-platform? 我在做什么错,我的跨平台在哪里?


UPD: Yes, it was my mistake with printing array. UPD:是的,这是我对打印阵列的错误。 But: Why it works differently with different directories? 但是:为什么在不同目录下它的工作方式有所不同? With first dir programm works fine, with second I got nullptr 与第一个目录编程工作正常,第二个我得到nullptr

maxim@maxim-VirtualBox:~$ java FileSearch /home/maxim/Downloads/archives/
maxim@maxim-VirtualBox:~$ java FileSearch /home/maxim/Install/
Exception in thread "main" java.lang.NullPointerException
    at FileSearch.saveFilesInList(FileSearch.java:21)
    at FileSearch.saveFilesInList(FileSearch.java:25)
    at FileSearch.saveFilesInList(FileSearch.java:25)
    at FileSearch.saveFilesInList(FileSearch.java:25)
    at FileSearch.saveFilesInList(FileSearch.java:25)
    at FileSearch.saveFilesInList(FileSearch.java:25)
    at FileSearch.saveFilesInList(FileSearch.java:25)
    at FileSearch.saveFilesInList(FileSearch.java:25)
    at FileSearch.main(FileSearch.java:88)

Here is my function: 这是我的功能:

    private static void saveFilesInList(String path, ArrayList<String> files)
                throws IOException
    {
            File dir = new File(path);
            File[] list = dir.listFiles();

[21]        for (File f : list) {
                if (f.isFile()) {
                    if (isUnic(f.getName(), files)) files.add(f.getName());
                } else {
[25]                saveFilesInList(f.getCanonicalPath(), files);
                }
            }
    }

both dirs have subdirs 两个目录都有子目录

UPD2: I found the problem. UPD2:我发现了问题。 listFiles() returns null, when directory is empty. 当目录为空时,listFiles()返回null。

You need to use a special method to print arrays. 您需要使用一种特殊的方法来打印数组。 :P Try :P试试

System.out.println(Arrays.toString(dir.listFiles()));

It does the same thing of Windows and Linux. 它具有Windows和Linux相同的功能。

I am not sure how the program is working properly in windows, it should not work in windows as well. 我不确定该程序如何在Windows中正常工作,它也不应该在Windows中正常工作。

The method dir.listFiles() returns array of File objects, therefore you must use something like: 方法dir.listFiles()返回File对象的数组,因此您必须使用类似以下内容的方法:

File[] files = dir.listFiles();

for (File file : files) {
    System.out.println(file);
}

to get correct output. 获得正确的输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM