Hardik Mishra
Hardik Mishra

Reputation: 14877

Escape Forward Slash While Creating FIle Java

I am creating some files and file names are being fetched from database.

There is a file name called

CUA/ICOR Digital Collection.

But, because of forward slash, "CUA" is getting treated as directory.

I have found Forward slash in Java Regex

and I have tried:

String fileName = "CUA/ICOR Digital Collection";
fileName = fileName.replaceAll("/", "\\\\/");  // OP: CUA\/ICOR Digital Collection But No success

fileName = fileName.replaceAll("/", Matcher.quoteReplacement("\\/"));  // OP: CUA\/ICOR Digital Collection But No success

fileName = fileName.replaceAll("/", Matcher.quoteReplacement("\\\\/"));  // OP: CUA\\/ICOR Digital Collection But No success

File file = new File(exportPath, fileName + ".xls");

I am getting

File Not Found Exception At 'C:\export\CUA\ICOR Digital Collection.xls'

So, Now I doubt is it possible ?

Upvotes: 3

Views: 9045

Answers (2)

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

If you are on Windows.... i don't think your file name can have variable like \

You can try converting that to a space or underscore....

So it would be like this...

CUA_ICOR Digital Collection

OR

CUA ICOR Digital Collection.

Upvotes: 0

Jeff
Jeff

Reputation: 3707

On Windows, a file can't contain /\:*?"<>|

You're better off converting the / character to something like an underscore (_)

Upvotes: 6

Related Questions