3

In my java program I copy a file and delete the new file.

In my method removeFile() I check if it is a directory:

String fileName = "G:/1310628353186Examples.csv";
File f = new File(fileName);
if (f.isDirectory()) {
    System.out.println( "'" + fileName + "' is a directory" );
    String[] files = f.list();
    if (files != null && files.length > 0)
        throw new IllegalArgumentException("Delete: directory not empty: " + fileName);
}

Sometimes I get "'G:/1310628353186Examples.csv' is a directory", sometimes I don't.

When I debug the code and f.isDirectory() is true and I check what is in f.isDirectory, the debugger says that it's false.

I'm running Eclipse SDK 3.4.0 and JDK 1.6 on Windows 7 Professional.

Vladimir Vagaytsev
  • 2,871
  • 9
  • 33
  • 36
Skyline
  • 63
  • 1
  • 8
  • What is `fileName`? Use `System.out.println( "'" + f + "' is a directory" );` to check what is in `f`! – user85421 Jul 14 '11 at 07:45
  • You say that `f.isDirectory()` is evalutated to true, but the debugger says it is false? This can't be true ;) – hage Jul 14 '11 at 07:59
  • I know :D I start the debugger... It jumps into the code of isDirectory()... But if i check isDirectory() with watch it says false... And f.list() is null. – Skyline Jul 14 '11 at 08:07
  • maybe it's worth trying `isFile()` instead of `isDirectory()` – hage Jul 14 '11 at 08:26
  • same problem... sometimes it's true... sometimes it's false. either isFile() is true or isDirectory() – Skyline Jul 14 '11 at 08:35
  • hmm last thing i can think of: What filesystem is on G: ? Does this behavior also occur with other files on that drive (and maybe on for the same file on another drive like C:)? – hage Jul 14 '11 at 08:41

6 Answers6

8

You check if f is a directory but you print fileName. So maybe you just check/print the wrong variable? Unless it's only a typo in your question.

hage
  • 5,966
  • 3
  • 32
  • 42
  • Good catch! I stared at that code block for a good 3 minutes and got nothing :) – Jon7 Jul 14 '11 at 07:45
  • i forgot the first lines. sorry:/ – Skyline Jul 14 '11 at 07:52
  • Then you should follow Carlos' hint and print `f` instead of `fileName` (and some of its properties, like `getPath()` or `getAbsolutePath()` or `getName()`. – hage Jul 14 '11 at 07:57
  • System.out.println("f: "+f); System.out.println("f.getPath(): "+f.getPath()); System.out.println("f.getAbsolutePath(): "+f.getAbsolutePath()); System.out.println("f.getName(): "+f.getName()); f: G:\1310631051940Example.csv f.getPath(): G:\1310631051940Example.csv f.getAbsolutePath(): G:\1310631051940Example.csv f.getName(): 1310631051940Beispiele.csv 'G:\1310631051940Example.csv' is a directory – Skyline Jul 14 '11 at 08:12
  • `f.getName(): 1310631051940Beispiele.csv` I assume you forgot to translate this one ;) But I have no idea why `isDirectory()` would return true... – hage Jul 14 '11 at 08:16
  • oh. yeah:/ should have been Example, too – Skyline Jul 14 '11 at 08:32
2

If the file is hidden, like the My Music link in Documents (a windows-generated shortcut file, it's not a real shortcut), then it will test as isdirectory but it won't function like a directory; you can't get a listing of the directory. A user generated shortcut will act like a file.

5/31/13: In a recent update the 'My Music' link in 'Documents' no longer tests as is_hidden. However, a dirlist taken of the file object for 'My Music' does return a NULL, which can be tested for.

Mary C
  • 79
  • 8
2

I get this same issue on OSX. I create a regular file, and the isDirectory() function returns true and isFile() returns false. (Can't find any evidence anywhere to suggest that this behaviour is expected from the API).

The way I solved it on OSX is to manually read the attributes from the file, but you can't use BasicFileAttributes either - it also for some reason returns true for isDirectory() and false for isRegularFile().

Works for OSX:

PosixFileAttributes attr = Files.readAttributes(f.toPath(), PosixFileAttributes.class);

System.out.println("File "+f.getName()+
                   ": isDirectory="+(attr.isDirectory() ? "true" : "false")+
                   ", isRegularFile="+(attr.isRegularFile() ? "true" : "false"));
Thane Thomson
  • 403
  • 1
  • 4
  • 14
2

Try adding a check to see if the file exists, isDirectory() will also return false in case the file doesn't exist:

if (f.exists() && f.isDirectory()) {
wjans
  • 10,009
  • 5
  • 32
  • 43
1

You have to sorround with try/catch block and then try it.

esef
  • 11
  • 1
  • At first, I only used if (files.length > 0) ... So I sometimes got a Null Pointer Exception. With if (files != null && files.length > 0) no more exceptions are thrown. – Skyline Jul 14 '11 at 08:25
1

It seemed to be a problem caused by Windows. Linux does not have this problem.

Skyline
  • 63
  • 1
  • 8