I've created both a File and Files object in Java based on a UNC path. When I check to see if the object isDirectory, both classes return false:
sourceDirectory = new File( "\\\\mymachine\\test\\new\\" );
boolean b = sourceDirectory.isDirectory();
Path path = sourceDirectory.toPath();
boolean a = Files.isDirectory( path );
results: b=false and a=false
What do I need to do to have my UNC directory recognized as a directory by File and Files?
Actually you are doing that correctly, but your local test case is misleading you.
For a windows path on a network share, your code would work fine, but for a local
path on your computer, you still need to specify the drive letter:
File netDir = new File("\\\\usstll0032\\share\\drc"); // network drive
System.out.println(netDir.isDirectory()); // true
File badDir = new File("\\\\us39-0cmq142\\temp"); // my computer
System.out.println(badDir.isDirectory()); // false
File goodDir = new File("\\\\us39-0cmq142\\c$\\temp"); // my computer
System.out.println(goodDir.isDirectory()); // true
Related
Is there a way to get user's home directory from java code?
For example: my user's home directory absolute path is C:\Users\stanislav. If I type %HOMEPATH% in Windows file explorer it will open me my user's directory.
I want to get this directory as File from JAVA using this code:
final Path homePath = Path.of("%HOMEPATH%");
final File file = homePath.toFile();
file.exists() // returns false
But this path is not referred to any file or directory. Help please.
String usersHomeDir = System.getProperty("user.home");
You can do
System.getProperty("user.home");
to get the home directory for the current user as a String. You can then pass the result to the File constructor to create a File object.
String home = System.getProperty("user.home");
System.out.println(home); // prints the path to your home directory
File file = new File(home);
System.out.println(file.exists()); // prints true
I am trying to load in a file and I am getting a FileNotFoundException even though the file is present. I have tried doing the absolute path (C:/Users/cdeck_000/AndroidStudioProjects/ProjectCaligula_Final/cert/cert.crt) and the relative path (cert/cert.crt) assuming Android starts at the project level. When I run it using the relative path and ask for the file absolute path I get this:
Path: /cert/cert.crt
The code is below along with the project structure.
File file = new File("cert/cert.crt");
boolean i = file.exists(); //false
boolean r = file.canRead(); //false
String path = file.getAbsolutePath(); //cert/cert.crt
String pathForApp = new File(".").getAbsolutePath(); //returns "/."
InputStream caInput = new BufferedInputStream(new FileInputStream(file)); //error
Can anyone chime in and let me know if my knowledge of absolute/relative paths with Android is wrong or give me advice on how to solve this? I have already thought that permissions was the issue but I raised the files permissions (equivalent to chmod 777) and it didn't change anything.
You should put it into src/cert/cert.crt, which makes it a resource, not a file, so you should use Class.getResourceAsStream("/cert/cert.crt"), not new FileInputStream().
My Question is how to move file not copy by just changing the path of file system level
in android file.nenameTo(newpath); this method works only when I have path like this
File f = new File(/storage/Folder1/Folder2/image.png);
File newfile = new File((/storage/Folder1/Folder3/image.png);
f.renameTo(newfile); // this method returns true
it works but when more then one parent folder change then it's not working
File f = new File(/storage/Folder1/Folder2/image.png);
File newfile = new File((/storage/Folder3/Folder4/image.png);
f.renameTo(newfile); // this method returns false
the following case also not work
File f = new File(/storage/Folder1/Folder2/image.png);
File newfile = new File((/storage/Folder3/image.png);
f.renameTo(newfile); // this method returns false
I want to move file like above
sorry for my English
You can only rename a file in Android if the src and dst are on the same mount point. You don't specify either way. Please consider using Files.move instead to avoid this potential issue and others.
I want to search for files in a directory. Therefore I want to get the directory in a File object but i'm getting a file instead of a directory. This is what I'm doing, it prints false but I want it to be true.
URL url = getClass().getResource("/strategy/viewconfigurations/");
File folder = new File(url.toString());
System.out.println(folder.isDirectory());
How can I load this way a directory?
It seems path or String you will got from the URL object cause problem.
You passed file path which you will got from the url.toString().
You need to change below line
File folder = new File(url.toString());
with this line
File folder = new File(url.getPath());
You need path of that folder which will you get from URL.getPath() function.
I hope this is what you need.
If you need an alternative for Java 7+ to Yagnesh Agola's post for finding a directory from a classpath folder, you could you also the newer java.nio.file.Path class.
Here is an example:
URL outputXml = Thread.currentThread().getContextClassLoader().getResource("outputXml");
if(outputXml == null) {
throw new RuntimeException("Cannot find path in classpath");
}
Path path = Paths.get(outputXml.toURI());
How do I get the directory name for a particular java.io.File on the drive in Java?
For example I have a file called test.java under a directory on my D drive.
I want to return the directory name for this file.
File file = new File("d:/test/test.java");
File parentDir = file.getParentFile(); // to get the parent dir
String parentDirName = file.getParent(); // to get the parent dir name
Remember, java.io.File represents directories as well as files.
With Java 7 there is yet another way of doing this:
Path path = Paths.get("d:/test/test.java");
Path parent = path.getParent();
//getFileName() returns file name for
//files and dir name for directories
String parentDirName = path.getFileName().toString();
I (slightly) prefer this way, because one is manipulating path rather than files, which imho better shows the intentions. You can read about the differences between File and Path in the Legacy File I/O Code tutorial
Note also that if you create a file this way (supposing "d:/test/" is current working directory):
File file = new File("test.java");
You might be surprised, that both getParentFile() and getParent() return null. Use these to get parent directory no matter how the File was created:
File parentDir = file.getAbsoluteFile().getParentFile();
String parentDirName = file.getAbsoluteFile().getParent();
File file = new File("d:/test/test.java");
String dirName = file.getParentFile().getName();
Say that you have a file called test.java in C:\\myfolder directory. Using the below code, you can find the directory where that file sits.
String fileDirectory = new File("C:\\myfolder\\test.java").getAbsolutePath();
fileDirectory = fileDirectory.substring(0,fileDirectory.lastIndexOf("\\"));
This code will give the output as C:\\myfolder