JDK-8156657 : Directory and file names ending with spaces are not supported by java.io.File
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 9
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows
  • CPU: generic
  • Submitted: 2016-05-10
  • Updated: 2025-04-11
  • Resolved: 2017-06-06
Related Reports
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
JDK 1.9.0 b116

ADDITIONAL OS VERSION INFORMATION :
64-bit MS Windows 7 OS

A DESCRIPTION OF THE PROBLEM :
The method "java.io.File.exists()" called on "java.io.File" object constructed from the absolute path to existing in the file system directory or file, whose name ends with space characters, for example "Directory1 ", "File1 ", "File2.txt ", always returns "false" value.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Compile the specified below test case.
2. Create in the file system a directory or a file, whose name ends with any number of space characters.
3. Run the compiled test case with a tested JDK and specify the absolute path to the created in the step #2 directory or file as the argument for the test case. For example:

"C:\Program Files (x86)\Java\jre-9\bin\java" FileNameEndingWithSpacesIsNotSupported "C:\Users\TestUser\AppData\Local\Temp\Directory1 "

---------- BEGIN SOURCE ----------
import java.io.File;

public class FileNameEndingWithSpacesIsNotSupported {
    public static void main(String[] args) {
        if (args.length > 0) {
            File file = new File(args[0]);
            System.out.println(String.format("File '%s' exists()='%b'", file, file.exists()));
        }
    }
}
---------- END SOURCE ----------
Comments
I don't think there is anything we can do here as this is not allowed on Windows. If you use the new file system API then you should get an InvalidPathException if you construct such as Path.
10-05-2016

The bug could not be reproduced on Linux OS with JDK 9 b117.
10-05-2016