This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Building Blockchain Apps and have Michael Yuan on-line!
See this thread for details.
Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • 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:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Create new file with spaces in file path

 
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 Mac OS X Firefox Browser
  • 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.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!