Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 701
Hardik Mishra Avatar asked Jul 03 '26 06:07

Hardik Mishra


1 Answers

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

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

like image 165
Jeff Avatar answered Jul 04 '26 20:07

Jeff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!