Read file with whitespace in its path using Java
Asked Answered
L

4

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.

Lawgiver answered 3/2, 2012 at 11:56 Comment(1)
possible duplicate of #5359350Northcutt
P
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

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

Proto answered 3/2, 2012 at 12:1 Comment(1)
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 elseProto
A
-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.

Anthologize answered 3/2, 2012 at 11:58 Comment(4)
files without spaces in their path are ok, i can open them...so permissions do not seem to be the issue :(.Lawgiver
@user253530: Show us the output from ls -l that lists the file in question.Anthologize
-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 thatLawgiver
@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.Anthologize
C
-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);
}
Chrono answered 17/5, 2017 at 15:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.