11

I am trying to open files with FileInputStream that have whitespaces in their names.

For example:

String fileName = "This is my file.txt";
String path = "/home/myUsername/folder/";

String filePath = path + filename;
f = new BufferedInputStream(new FileInputStream(filePath));

The result is that a FileNotFoundException is being thrown. I tried to hardcode the filePath to "/home/myUserName/folder/This\\ is\\ my\\ file.txt" just to see if i should escape whitespace characters and it did not seem to work. Any suggestions on this matter?

EDIT: Just to be on the same page with everyone viewing this question...opening a file without whitespace in its name works, one that has whitespaces fails. Permissions are not the issue here nor the folder separator.

jww
  • 97,681
  • 90
  • 411
  • 885
user253530
  • 2,583
  • 13
  • 44
  • 61
  • possible duplicate of http://stackoverflow.com/questions/5358850/accessing-files-with-spaces-in-filename-from-java – olly_uk Feb 03 '12 at 11:59

4 Answers4

5

File name with space works just fine

Here is my code

File f = new File("/Windows/F/Programming/Projects/NetBeans/TestApplications/database prop.properties");
        System.out.println(f.exists());
        try
        {
            FileInputStream stream = new FileInputStream(f);
        }
        catch (FileNotFoundException ex)
        {
            System.out.println(ex.getMessage());
        }

f.exists() returns true always without any problem

Sunil Kumar B M
  • 2,735
  • 1
  • 24
  • 31
  • thanks for showing me that i first have to use File(path) before sending it to FileInputStream. – user253530 Feb 03 '12 at 12:16
  • 1
    @user253530: Are you implying that your original code did not work, and wrapping `filePath` into a `File()` made it work?! – NPE Feb 03 '12 at 12:18
  • 2
    yes...if i use FileInputStream(String filePath) it does not work....but if i first do File f = new File(filePath) and then FileInputStream(f) does not throw an error...very weird.. – user253530 Feb 03 '12 at 12:23
  • 2
    That doesn't make sense - the constructor for `FileInputStream(String name)` just creates a `File` anyway - the code is equivalent. – DNA Feb 03 '12 at 15:47
1

Looks like you have a problem rather with the file separator than the whitespace in your file names. Have you tried using

System.getProperty("file.separator")

instead of your '/' in the path variable?

Kris
  • 5,714
  • 2
  • 27
  • 47
  • don't escape your whitespace, I belive you are on unix/linux like env. If it doesn't work you are doing somethig else wrong eg. mispelling the file name or smth else – Kris Feb 03 '12 at 12:22
-1

Normally whitespace in path should't matter. Just make sure when you're passing path from external source (like command line), that it doesn't contain whitespace at the end:

File file = new File(path.trim());

In case you want to have path without spaces, you can convert it to URI and then back to path

try {
    URI u = new URI(path.trim().replaceAll("\\u0020", "%20"));
    File file = new File(u.getPath());
} catch (URISyntaxException ex) {
    Exceptions.printStackTrace(ex);
}
Tombart
  • 30,520
  • 16
  • 123
  • 136
-1

No, you do not need to escape whitespaces.

If the code throws FileNotFoundException, then the file doesn't exist (or, perhaps, you lack requisite permissions to access it).

If permissions are fine, and you think that the file exists, make sure that it's called what you think it's called. In particular, make sure that the file name does not contain any non-printable characters, inadvertent leading or trailing whitespaces etc. For this, ls -b might be helpful.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • files without spaces in their path are ok, i can open them...so permissions do not seem to be the issue :(. – user253530 Feb 03 '12 at 12:00
  • 1
    @user253530: Show us the output from `ls -l` that lists the file in question. – NPE Feb 03 '12 at 12:00
  • -rw-r--r-- 1 root root 101 2012-02-03 11:16 add.txt -rw-r--r-- 1 root root 13969 2012-02-02 20:23 CASE 1A.txt everyone has read permissions...this is not the issue...i can open add.txt but i cannot open CASE 1A.txt ... it is the name i am sure of that – user253530 Feb 03 '12 at 12:06
  • @user253530: I am certain that the embedded whitespace is not the problem. Make sure that the file name does not contain any non-printable characters, inadvertent leading or trailing whitespaces etc. `ls -b` might be helpful. – NPE Feb 03 '12 at 12:12