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
Related
I tried this link:
How to create text file and insert data to that file on Android
But, it says "No such file or directory". Can anybody help me please? Thanks in advance!
---- Updated answer----
I think you can't create a directory inside the internal storage of the device. Except you've a root access for the app. You can only create the directory inside your app private folder within the following path String path = getFilesDir().
you can use like this below -
File mydir = context.getDir("mydirectory", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myAwesomeFile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
getDir(StringName, int mode) method to create or access directories in internal storage.
/storage/emulated/0/Notes/ will always return No such file or directory except device is rooted and dir.mkDirs() will always return false for this path.
Hope this will help you.
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());
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
I am using the following piece of code to create a directory in java, under Linux:
String dir = "~/tempDir/";
if (!IOUtils.createDirectory(dir)) {
throw new IOException("could no create the local store directory: "
+ dir );
}
LOGGER.info("local store successfully created.");
The application seems to create the directory, as I get no errors and it is working fine.
The problem is that I cannot see this directory on the disk; I am looking in my home directory.
I need to mention that this is a java web application running under tomcat.
Does anyone have any idea why I cannot see this directory?
This does not work because ~ is expanded by your shell, bash or sh or whatever. This doesn't work from Java.
You have created a directory called ~ in your working directory.
You need to get the user's home directory from the system property user.home and build your path from that.
final File dir = new File(System.getProperty("user.home"), "tempDir");
The following will create a "New Folder" under Home, if a folder with the specified name does NOT exist already
final String homePath = System.getProperty("user.home") + "/";
final String folderName = "New Folder";
File file = new File(homePath + folderName);
if (!file.exists())
file.mkdir();
Another option (a better one I should say) you have is using another constructor of File that takes a parent path and a child path as follows:
final String homePath = System.getProperty("user.home");
final String folderName = "New Folder";
File file = new File(homePath, folderName);
if (!file.exists())
file.mkdir();
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