File testDir = new File("C:\temp\test");
testDir.createNewFile();
As I understand it, the above will create a directory called test in the directory c:\temp
File testDir = new File("C:\temp\test.dir");
testDir.createNewFile();
As I understand it, the above will create a file called test.dir in the directory c:\temp
What should I be doing to the code above if I wish for test.dir to actually be a directory?
No, the first one will create a regular file - after all, that's what you asked it to do:
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.
Nothing there says it will create a directory. You'll want to escape the backslashes though, or it's trying to find C:<tab>emp<tab>est
If you want to create a directory, use File.mkdir or File.mkdirs(). You'll still need to escape the backslashes:
File testDir = new File("C:\\temp\\test.dir");
bool created = testDir.mkdir();
(Use mkdirs to create parent directories as well.) The return value tells you whether or not it actually created a directory.
That's not true.
File.createFile() will create a file.
File.mkdir() creates a directory.
http://download.oracle.com/javase/6/docs/api/java/io/File.html
File testDir = new File("C:\temp\test");
testDir.createNewFile();
As I understand it, the above will
create a directory called test in the
directory c:\temp
Wrong - it will create file called "test". Files do not have to have a "filename extension".
To create a directory:
testDir.mkdir();
BTW, this kind of question is most easily and quickly answered by looking at the API doc. Do yourself a favor and get familiar with it.
Related
I'm working with Java 1.8. I'm trying to create a folder if not exists using this method:
private void createDirIfNotExists(String dirChemin) {
File file = new File(dirChemin);
if (!file.exists()) {
file.mkdirs();
}
}
This works when I give it the right path, for example this creates a folder if it doesn't exist
createDirIfNotExists("F:\\dir")
But when I write an incorrect path (or name), it didn't give me any thing even an error! for example :
createDirIfNotExists("F:\\..?ยง;>")
So I want to improve my method, so it can create the folder if it doesn't exist by making sure that my path is right, otherwise it should give me an error message.
mkdirs() also creates parent directories in the path this File represents.
javadocs for mkdirs():
Creates the directory named by this abstract pathname, including any
necessary but nonexistent parent directories. Note that if this
operation fails it may have succeeded in creating some of the
necessary parent directories.
javadocs for mkdir():
Creates the directory named by this abstract pathname.
Example:
File f = new File("non_existing_dir/someDir");
System.out.println(f.mkdir());
System.out.println(f.mkdirs());
will yield false for the first [and no dir will be created], and true for the second, and you will have created non_existing_dir/someDir
This is the problem I have: If part or all of the path does not already exist, the server should create additional directories as necessary in the hierarchy and then create a new file as above.
Files.createDirectories(path);
That's what I am currently using, but it does not create the end file. For example is the path="/hello/test.html" it will create a directory called "hello" and one called "test.html", I want the test.html to be a file. How can I do that?
This is what I did to solve this "problem" or misuse of the libraries.
Files.createDirectories(path.getParent());
Files.createFile(path);
The first line will get the parent directory, so lets say this is what I want to create "/a/b/c/hello.txt", the parent directory will be "/a/b/c/".
The second like will create the file within that directory.
Have you looked at the javadoc? createDirectories only creates... directories. If you're intent on using Files.createDirectories, parse off the file name, call createDirectories passing only the path portion, then create a new file passing the entire path. Otherwise this is a better approach.
Files.createDirectories(path.substring(0, path.lastIndexOf(File.separator)+1));
File yourFile = new File(path);
you can parse the 'path' variable to isolate the file and the directory using delimiter as '/', and do File file = new File(parsedPath); This would work only when you know that you ALWAYS pass the file name at the end of it.
If you know when you are a) creating a directory b) creating a directory and file, you can pass the boolean variable that would describe if file needs to be created or not.
I'm new to java, I got the path from the user, using chooser.getCurrentDirectory(), now i want to use the directory to create a file there, File report = new File(chooser directory + "filename"), but it only accepts string, not file, so how can i get the chooser directory as a string?
You should not use chooser.getCurrentDirectory() to start off with, you should use chooser.getSelectedFile();.
And you should take a look at http://docs.oracle.com/javase/7/docs/api/java/io/File.html, specifically at the get*() methods that involve the filename.
I have a directory which contains several files and directories. I am writing a small java program which displays the files present in a the directory supplied as a parameter.
The problem I am facing is when I append dot(s) after a directory name, it is being treated as existing even if the directory is not present. To further clarify, suppose I have a directory named "abc" which exists. It works fine when I enter "abc". But when I enter the directory name as "abc...", even then also the directory is being treated as it exists. I want to avoid it. I am creating a FIle object using
File directory = new File( fileName );
if ( directory.exists() ) {
// do something
}
Any suggestions how can I avoid it?
This is unrelated to Java, it's a Windows thing: Trailing dot(s) are removed from file and folder names. Even C/C++ programs can't do it.
As a workaround, try to use the prefix \\?\:
File dir = new File( "\\\\?\\" + path );
But this will disable a lot of other things like relative paths and slash conversion.
Related answers:
How to create a filename with a trailing period in Windows?
MSDN Naming Files, Paths, and Namespaces
Why doesn't Explorer let you create a file whose name begins with a dot?
Is it possible to create directory in directory. To create one directory simply call this:
File dir1 = getDir("dir1",Context.MODE_PRIVATE);
But how to create other directory in dir1 ?
this:
File dir2 =getDir("dir1"+"/"+"dir2",Context.MODE_PRIVATE);
throw exception:
File dirFile = java.lang.IllegalArgumentException: File app_dir1/dir2 contains a path separator
Thanks.
The Context.getDir() appears to be an Android-unique method for abstracting out the process of creating a directory within the private storage area - it is not a generic way of making directories in general.
To make your child subdirectory, you should use normal java methods, such as
File dir2 =new File(dir1, "dir2").mkdir();
Note that the first parameter here is the File Object representing the first directory you created, not the name.
You may want to subsequently set the permissions on this directory.
warning: untested