Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create new file with spaces in file path

 
Jalal ud Din
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check if you have escaped "\" with "\\".
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Directory does already exist.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Trick is to go over URI, that is handled the same in all operating system.



Edit: Added the createParents utility method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic