aspose file tools*
The moose likes Java in General and the fly likes Create new file with spaces in file path Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Create new file with spaces in file path" Watch "Create new file with spaces in file path" New topic
Author

Create new file with spaces in file path

Jalal ud Din
Greenhorn

Joined: Dec 20, 2006
Posts: 24
I am trying to create a file on a path which contains spaces in directory names. like I want to created a file at "c:\abc def\a.txt". File.createNewFile() throws the excpation that The system cannot find the path specified. Please help. Directory does already exist.

java version "1.6.0_16"

code looks like as follows.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
check if you have escaped "\" with "\\".
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
possible cause of exception : there is no directory called abc def at the path specified.

Solution: create directory first and put a new file in that directory.

File dir = new File("c:\\abc def");
dir.mkdir();
File file = new File(dir,"abc.txt");
file.createNewFile();
Jalal ud Din
Greenhorn

Joined: Dec 20, 2006
Posts: 24
Directory does already exist.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
If you post the code you try it would be bit helpful..
Never mind... I just saw you have edited and posted the code...
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I tried your code and got the below error when the directory "abc def" was not present. Print the stack trace and see the error generated.
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(File.java:883)
at ranch.CreateFile.main(CreateFile.java:13)
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
if directory already exists please check the number of spaces between abc and def and make sure you specify the directory name exactly the same. I don't see any other reason for exception
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Trick is to go over URI, that is handled the same in all operating system.



Edit: Added the createParents utility method.
 
Consider Paul's rocket mass heater.
 
subject: Create new file with spaces in file path