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.

1

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for showing me that i first have to use File(path) before sending it to FileInputStream.
@user253530: Are you implying that your original code did not work, and wrapping filePath into a File() made it work?!
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..
That doesn't make sense - the constructor for FileInputStream(String name) just creates a File anyway - the code is equivalent.
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?

1 Comment

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
-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.

4 Comments

files without spaces in their path are ok, i can open them...so permissions do not seem to be the issue :(.
@user253530: Show us the output from ls -l that lists the file in question.
-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: 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.
-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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.