Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing files with spaces in filename from Java

I want to access files in a directory which have spaces in filename from a java program but it doesnot access file.

Scenario is i have names of file in a file . iread file names from that file and not able to open files with spaces in java .

We are using File.exist function to check if file exist but it return false.

i have tried several kind of formats to represent spaces llike "ab\ c"for file name ab c and ab%20c for same file.

but nothing is helping.

like image 390
Gaurav Avatar asked Mar 18 '11 23:03

Gaurav


1 Answers

I had the similar problem while trying to read a resource location from Servlet on a Windows OS. The following line worked for me.

String path = config.getServletContext().getResource("/app").toString();
path = URLDecoder.decode(path,"UTF-8");
path = path.replaceAll("file:/", "");
path = path.replaceAll("\\u0020", "\\ ");
File verLocation = new File(path);

After all of the above line the verLocation.exists() is returning true else was also false.

like image 189
Avikar Avatar answered Sep 27 '22 21:09

Avikar