JDK-6222304 : REGRESSION: create a file with long file names in Windows XP
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2005-01-27
  • Updated: 2011-02-16
  • Resolved: 2005-02-04
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.4.2_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_06-b03)
Java HotSpot(TM) Client VM (build 1.4.2_06-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
I am able to create a file which is invalid in Windows XP. In windows XP, the maximum allowed size for a file name is 255(It includes the absolute path and not just the file name). But Java allows me to create file whose absolute filename length is greater than 255. After the file creation, I can't delete this file in windows(I can't do anything with this file).

Please refer to the following code.

public static void main(String[] args)
	{
		GenerateFileWithLongName obj = new GenerateFileWithLongName();
		obj.generate();
	}
	public void generate(){
		String longFileName;
		longFileName = getLongName(220); //Create a name with 220 characters

		try{
			//System.getProperty("user.dir") = "C:\Documents and Settings\kiran_ms\Desktop" (Length=40(approx))
			BufferedWriter br = new BufferedWriter(new FileWriter(new File(System.getProperty("user.dir")+File.separator+longFileName+".txt")));
			//So absolute path length is graeter than 255
			br.write("first line");
			br.flush();
			br.close();
			System.out.println("Created the file");
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	private String getLongName(int count){
		String name = "";
		for (int i=0; i<count; i++){
			name += "a";
		}
		return name;
	}

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Create a java class GenerateFileWithLongName.java
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

public class  GenerateFileWithLongName
{
	public static void main(String[] args)
	{
		GenerateFileWithLongName obj = new GenerateFileWithLongName();
		obj.generate();
	}
	public void generate(){
		String longFileName;
		longFileName = getLongName(220); //Create a name with 220 characters

		try{
			//System.getProperty("user.dir") = "C:\Documents and Settings\kiran_ms\Desktop" (Length=40(approx))
			BufferedWriter br = new BufferedWriter(new FileWriter(new File(System.getProperty("user.dir")+File.separator+longFileName+".txt")));
			//So absolute path length is graeter than 255
			br.write("first line");
			br.flush();
			br.close();
			System.out.println("Created the file");
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	private String getLongName(int count){
		String name = "";
		for (int i=0; i<count; i++){
			name += "a";
		}
		return name;
	}
}
2. Save it in a directory whose absolute path length is greater than 35 characters(Eg: "C:\Documents and Settings\kiran_ms\Desktop")
3. Compile and run the program.
4. Now go to "C:\Documents and Settings\kiran_ms\Desktop" and try to delete the file.
5. You can't do anything with this file !!!

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
I was expecting an exception thrown when I tried to create the file.
ACTUAL -
File got created, which is an invalid file in Windows XP environment.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
No error message

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.IOException;

public class  GenerateFileWithLongName
{
	public static void main(String[] args)
	{
		GenerateFileWithLongName obj = new GenerateFileWithLongName();
		obj.generate();
	}
	public void generate(){
		String longFileName;
		longFileName = getLongName(220); //Create a name with 220 characters

		try{
			//System.getProperty("user.dir") = "C:\Documents and Settings\kiran_ms\Desktop" (Length=40(approx))
			BufferedWriter br = new BufferedWriter(new FileWriter(new File(System.getProperty("user.dir")+File.separator+longFileName+".txt")));
			//So absolute path length is graeter than 255
			br.write("first line");
			br.flush();
			br.close();
			System.out.println("Created the file");
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	private String getLongName(int count){
		String name = "";
		for (int i=0; i<count; i++){
			name += "a";
		}
		return name;
	}
}
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
None, Except mannually checking for  the length(Which will be platform specific)

Release Regression From : 1.4.1_08
The above release value was the last known release where this 
bug was known to work. Since then there has been a regression.
###@###.### 2005-1-27 13:12:19 GMT

Comments
EVALUATION duplicate of 4403166. the fact that we partically support long filepath on windows in 1.4.x ("partically" means that some apis do, some apis don't) causes confusion. With the fix of 4403166, now long filepath (longer than 260, but each component of the path still has the 260 limit) is fully supported by java.io.File class on nt based windows platforms . ###@###.### 2005-2-04 07:59:28 GMT
04-02-2005