Question

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. I have something like this:

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

Launch at Windows (works fine):

java MyProg C:\mydir

Launch at 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. But: Why it works differently with different directories? With first dir programm works fine, with second I got 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. listFiles() returns null, when directory is empty.

No correct solution

OTHER TIPS

You need to use a special method to print arrays. :P Try

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

It does the same thing of Windows and Linux.

I am not sure how the program is working properly in windows, it should not work in windows as well.

The method dir.listFiles() returns array of File objects, therefore you must use something like:

File[] files = dir.listFiles();

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

to get correct output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top