• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Create new file with spaces in file path  RSS feed

 
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.

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check if you have escaped "\" with "\\".
 
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
 
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
Boost this thread!