JDK-6684955 : JFileChooser does not correctly handle files with prefixing space char
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86
  • Submitted: 2008-04-07
  • Updated: 2011-01-19
  • Resolved: 2008-08-27
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.6.0"
Java(TM) SE Runtime Environment (build 1.6.0-b105)
Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux netbrain 2.6.22-3-686 #1 SMP Sun Feb 10 20:20:49 UTC 2008 i686 GNU/Linux

A DESCRIPTION OF THE PROBLEM :
Issue 83: torrent file does not load
http://code.google.com/p/ntorrent/issues/detail?id=83

Comment #3 by kei060:
congratulations, i think you have found a bug in java.

ok, heres my test setup.
i created the files " music.torrent" and "music .torrent" to test with spaces.

and in the add torrent procedure i've added a simple printout on the
paths of these
files as they are submitted to the procedure. if i select the file " music.torrent"
and press add, the output is:
/home/netbrain/music.torrent, where the space has been omitted. Why?


and then i tried with the other torrent where the output was:
/home/netbrain/music .torrent, worked great, and this is were it gets interesting  :)

i then multiselect both files and get the output:
/home/netbrain/ music.torrent
/home/netbrain/music .torrent

So the only thing i can conclude from this is that there is something
wrong with
java. not nTorrent. It seems that with a single file selection the
space is omitted
by JFileChooser (only works on files with a space as the first char in
their name).
but if i select multiple files the space is included.

Here's a code you can test it out for yourself.

import java.io.File;
import javax.swing.JFileChooser;


/**
 * This class demonstrates a bug in java where if a file " file1" (
 * with a single space as a prefix) and "file2" exists in the system.
 * Then with a single selection of " file1" will actually give a path to "file1"
  * (without a single space as a prefix), but if you select both "
file1" and "file2",
 * then JFileChooser will result in the correct and valid files.
 */
public class JFileChooserBugDemo {
	public static void main(String[] args) {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(true);
		int result = chooser.showOpenDialog(null);
		if(result == JFileChooser.APPROVE_OPTION){
			for(File f : chooser.getSelectedFiles()){
				System.out.println(f);
			}
		}
	}

}



STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
/**
 * This class demonstrates a bug in java where if a file " file1" (
 * with a single space as a prefix) and "file2" exists in the system.
 * Then with a single selection of " file1" will actually give a path to "file1"
  * (without a single space as a prefix), but if you select both "
file1" and "file2",
 * then JFileChooser will result in the correct and valid files.
 */
public class JFileChooserBugDemo {
	public static void main(String[] args) {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(true);
		int result = chooser.showOpenDialog(null);
		if(result == JFileChooser.APPROVE_OPTION){
			for(File f : chooser.getSelectedFiles()){
				System.out.println(f);
			}
		}
	}

}

1. use the above code.
2. first select " file1" with prefixing space char.
3. the select  "file2" without prefixing space char.
4. then multi select both files.
5. look at the output for each instance.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Selection of  " file1"
"/path/to/ file1"
Selection of "file2"
"/path/to/file2"
Selection of both files
"/path/to/ file1"
"/path/to/file2"


ACTUAL -
Selection of  " file1"
"/path/to/file1"
Selection of "file2"
"/path/to/file2"
Selection of both files
"/path/to/ file1"
"/path/to/file2"

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
import javax.swing.JFileChooser;

/**
 * This class demonstrates a bug in java where if a file " file1" (
 * with a single space as a prefix) and "file2" exists in the system.
 * Then with a single selection of " file1" will actually give a path to "file1"
  * (without a single space as a prefix), but if you select both "
file1" and "file2",
 * then JFileChooser will result in the correct and valid files.
 */
public class JFileChooserBugDemo {
	public static void main(String[] args) {
		JFileChooser chooser = new JFileChooser();
		chooser.setMultiSelectionEnabled(true);
		int result = chooser.showOpenDialog(null);
		if(result == JFileChooser.APPROVE_OPTION){
			for(File f : chooser.getSelectedFiles()){
				System.out.println(f);
			}
		}
	}

}


---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Don't use filenames with prefixed space char :)