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
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
I've tried several different things to get this to work, and none of them have. I'm trying to create a file inside of a folder in Java. The project needs to have several text files that all relate to each other, and it would be more manageable to have them all together in one folder. Ideally, this folder would be stored outside of scr/.
Here is my current code for it (I do check for file existence first):
File testFile = new File("\\appts\\Appointments" + name + ".txt");
try {
testFile.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
However, I get an IOException when I try to run the code. I have tried it how it is above, also /appts/Appointments, appts/Appointments, appts\\Appointments. I tried searching online but couldn't find anything that worked.
Edit: Here is how my project setup currently looks like:
Project_Folder
src
com
weebly
roboticplayer
appointmentbook
CLASSES
Here is how I want it to look:
Project_Folder
src
com
weebly
roboticplayer
appointmentbook
CLASSES
appts
There are two easy ways to do this:
1) Absolute path
"C:\\users\\....\\parent_folder\\filename.txt";
2) relative path,
. (Single dot) is current directory
..(double dots) is parent directory
For example, you want to create text files under project folder. And the following is your file structure.
Project_folder
src
Java_main_file.java
appts
You want to create a file under appts from Java_main_file.java
String filename = "..\\appts\\filename.txt"
Then, create your file with filename. Here is a link how to create a text file.
Note that you need to make sure the folder under which you create the files exists. If it doesn't exist, you will get an error.
You can't create a file and its non-existent parent directories in one step, but you can create the parent directories first with File.mkdirs() and then create the file.
If your using JDK7 you can use nio package.
Path path = Paths.get("C:\\appts\\Appointments");
Files.createDirectories(path);
If you want to do a path relative to your current folder:
FileSystems.getDefault().getPath("appts", "Appointments");
If you want to see the absolute pathname:
FileSystems.getDefault().getPath("appts", "Appointments").toAbsolutePath().toString();
If you need that file object:
FileSystems.getDefault().getPath("appts", "Appointments").toFile();
You can also do .toFile() after the call toAbsolutePath().
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 am currently creating a directory based on file names and then moving the files into the new directories. At the moment i am creating the new directories fine using the following code:
String filename = filesInFolder.get(i).toString();
File fullPathFile = new File(filename.replaceAll("(\\w+)_(\\d+).*", "$1/$2/$0"));
fullPathFile.getParentFile().mkdirs();
Then i am trying to use InputStream and OutputStream to move the files to the new directories, the code is ok it seems but when i create the new directories, all the folders are set to read-only so i cannot move the files into the knew directories
So is there a way to set the folders to read-write when they are created?
I believe fullPathFile.getParentFile().setWritable(true) before calling mkdirs() should do.
The method setWritable (bool) is a convenience method to set the owner's write permission for this abstract pathname. Since is a File, you can apply it.
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.