Reputation: 33
My task is to save a file in the path specified by the end user (must be an absolute path). Am currently facing three different scenarios as in below:
For the first case, I need to save the file in a default location. For the second, I need to create directories and save the file. And for the last, I'll save in the specified path.
Below is my code snippet which is saving the file in default location for both first and second scenarios. Am having difficulty to distinguish between first and second. java.io.File.mkdirs works for the second case, but not for the first. Please ignore my poor code as am new to this programming language. Any inputs would be much appreciated.
//User input must be absolute path
String saveToFolder = "kut igeiguye jh";
String defaultFolder = "C:\\Parent\\Data";
try{
File file = new File(saveToFolder);
if(!file.exists()){
saveToFolder = defaultFolder;
}
file.mkdirs();
}catch(Exception e){
saveToFolder = defaultFolder;
}
//code to save data in path **saveToFolder**
Upvotes: 2
Views: 6511
Reputation: 6123
Java NIO make this simple. The method Files.createDirectories succeeds whether the directory already exists or didn't exist and has been created.
try {
Files.createDirectories(Paths.get(saveToFolder));
}
catch (IOException e) {
System.err.println("Using default folder due to the following exception: " + e);
saveToFolder = defaultFolder;
}
If you really want to check whether the user entered an absolute path, just use Path.isAbsolute().
Upvotes: 0
Reputation: 4669
you can use isFile() to valid over exists(). I have not checked for all cases but this should help you.
public static void checkPath(){
String saveToFolder = "file/User.txt";
String defaultFolder = "file/data/";
try{
File file = new File(saveToFolder);
if(!file.isFile()){
saveToFolder = defaultFolder;
System.out.println("File not found");
}
file.mkdirs();
}catch(Exception e){
saveToFolder = defaultFolder;
}
}
Upvotes: 0
Reputation: 33
Appreciate all those who added their inputs. I made few changes to my code which works. One of my assignments requirement was that my program must take absolute path as input. With my below code, am able to save data in default location for case 1; New folders are being created and file is being saved in the newly created folder for case 2; And saving the file in specified path for case 3.
String saveToFolder = "kut igeiguye jh";
String defaultFolder = "C:\\Parent\\Data";
try{
File file = new File(saveToFolder);
boolean dirCreated = file.mkdirs();
if(!dirCreated || (file.getParent().equals(null))){
saveToFolder = defaultFolder;
}
}catch(Exception e){
saveToFolder = defaultFolder;
}
System.out.println("save to folder : " +saveToFolder);
Upvotes: 0
Reputation: 1812
1) For 1st case use regex to determine if path is valid or not
String regularExpression = "([a-zA-Z]:)?(\\[a-zA-Z0-9_-]+)+\\?" ; // this regex for windows. If you are running in linux then regex will be different.
Pattern pattern = Pattern.compile(regularExpression);
boolean isMatched = Pattern.matches(regularExpression,saveToFolder);
2) Check if path is valid or not again using 1st method. If its valid check if folder exists or not.
File f = new File(saveToFolder);
if (f.exists() && f.isDirectory()) {
...
}
3) If path is valid by first method and if it exists second method .
Upvotes: 2
Reputation: 5449
kut igeiguye jh is happily accepted by most file systems out there. If you don't want spaces in your path/filenames, you already need to check the validity by yourself e.g. using a Regular Expression.
Upvotes: 1