3

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 ?

Community
  • 1
  • 1
Hardik Mishra
  • 14,779
  • 9
  • 61
  • 96

2 Answers2

5

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

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

kosa
  • 65,990
  • 13
  • 130
  • 167
Jeff
  • 3,669
  • 1
  • 23
  • 33
  • 1
    ANd What about Linux, I am teting it on Windows but have to run in Linux – Hardik Mishra Sep 13 '12 at 18:15
  • 1
    Every OS i'm aware of (Windows and all *nix variants, including Linux and likely OSX) interprets `/` as a directory separator, and consequently doesn't allow it in file names except where it's used as a directory separator. – cHao Sep 13 '12 at 18:16
  • I think in *nix the `/` is invalid/reserved for file separator. I would do the same replacement regardless of OS. – Jeff Sep 13 '12 at 18:18
  • @cHao: Oh I remember Yes, / is file separator in Linux. I guess I have to convert the `/`char to something like an underscore – Hardik Mishra Sep 13 '12 at 18:19
  • @Jeff: Pretty much. AFAIK the only two chars not allowed in a file's name in Linux are NUL (`'\0'`) and slash. – cHao Sep 13 '12 at 18:20
  • @cHao - I Windows, the `/` is not a path separator; it is the start of a switch. So `dir/w` in Windows (no space needed) is like `ls -l` in *nix. – Ted Hopp Sep 14 '12 at 02:25
  • @Ted: Dunno where you've been the past 17 years, but Win32 has accepted `/` as a path separator (probably internally converting it to `\\`, but who cares) for quite some time now. The only time it gets a little weird about it is outside quotes after a space. – cHao Sep 14 '12 at 03:57
  • Apparently, [it goes back even further than that](http://bytes.com/topic/python/answers/23123-when-did-windows-start-accepting-forward-slash-path-separator). – cHao Sep 14 '12 at 04:03
0

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.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75