vargen_
vargen_

Reputation: 2790

java File.separator becomes "%" in path of the File on Windows

I try to read files from the resources folder. The problem is, that the File.separator turns into a "%" on Windows.

String inputFilesFolder = "input_files" + File.separator;
File file = new File(classLoader.getResource(inputFilesFolder + "filename").getFile());

The inputFilesFolder is still fine (input_files/), but after creating the file file.getPath() becomes D:\blabla\input_files%filename.

Then I try to read the file, but I get a FileNotFoundException (big surprise). What's wrong here?

Upvotes: 1

Views: 2337

Answers (3)

Mehrad Sadegh
Mehrad Sadegh

Reputation: 1134

File.separator is a file system thing. When you're using classLoader.getResource() always use forward slash as the name of a resource is a '/'-separated path name.

See Javadoc for getResource()

Upvotes: 5

user4624062
user4624062

Reputation:

How about

 String inputFilesFolder = "input_files" + File.separator;
 File file = new File(classLoader.getResource(inputFilesFolder + filename).toString());

Upvotes: 0

Paul Vargas
Paul Vargas

Reputation: 42020

Try this:

File file = new File(classLoader.getResource(inputFilesFolder + filename).toURI());

Upvotes: 2

Related Questions