Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to create a folder with spaces written in Java under Linux?

User_KFTB2Feb 16 2013 — edited May 30 2013
Hello,

I have a serious problem

I want to run a Linux command using a Java class with the runtime interface, there is a command to create a folder named eg "My Folder", with a space

For create the Unix command is easy to do either:
mkdir My\ Folder
or
mkdir "My Folder"

But how to translate this in Java, I tried with two commands :
Runtime.exec("mkdir My\\ Folder")
Runtime.exec("mkdir \"My Folder\"")

For example :

import java.io.IOException;
public class CreerDossier {
public static void main(String[] args) throws IOException {
Runtime runtime = Runtime.getRuntime();
runtime.exec("mkdir My\\ Folder");
runtime.exec("mkdir \"My Folder\"");
}
}

But it's still not working,

For runtime.exec("mkdir My\\ Folder") it creates two folders My\ and Folder
For runtime.exec("mkdir \"My Folder\"") it creates also two folders "My and Folder"

Are there solutions?

Thank you !
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 18 2013
Added on Feb 16 2013
5 comments
1,230 views