This question already has answers here:
How to get just the parent directory name of a specific file
(10 answers)
Closed 4 years ago.
It should be rather easy, but I could not find a clean way to do so. I'm trying to get the parent full path of parent directory in a path. Consider the following path: /bin/src/config/file, I would like to get /bin/src/config in Java. So I get a string and need to get the full path (absolute path, not just the name and not relative) of the parent directory. What is the cleanest way to do so?
You can use this, which will print the folder your file is in where fileName is your filename:
Path f = Paths.get(fileName);
System.out.println(f.getParent());
Eg for String fileName = "C:\\Users\\Me\\Documents\\video.html" the output is C:\Users\Me\Documents.
Related
This question already has answers here:
How to get the real path of Java application at runtime?
(15 answers)
Closed 6 years ago.
I'm trying to load a .txt file into an arrayList in java using a combination of relative paths.
My jar file is in /usr/tool/dist/tool.jar
The file I want to load is in /usr/tool/files/file.txt
I think I was able to retrieve the path of my tool.jar, but how can I go from that path to the one where my file is?
I have the following code
// String path should give me '/usr/tool'
File f = new File(System.getProperty("java.class.path"));
File dir = f.getAbsoluteFile().getParentFile();
String path = dir.toString();
String table1 = this should represent /usr/tool/files/file.txt
BufferedReader buf_table1 = new BufferedReader(new FileReader(new File(table1)));
To find the path of your jar file being executed, java.class.path is not the right property. This property may contain more than one file, and you cannot know which is the right one. (See the docs.)
To find the path of the correct jar file, you can use this instead:
URL url = MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath();
Where MainClass the main class of your tool, or any other class in the same jar file.
Next, the parent File of a file is its directory. So the parent File of /usr/tool/dist/tool.jar is /usr/tool/dist/. So if you want to get to /usr/tool/files/file.txt, you need to get the parent of the parent, and then from there files/file.txt.
Putting it together:
File jarFile = new File(MainClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
File file = new File(jarFile.getParentFile().getParent(), "files/file.txt");
This question already has answers here:
getResourceAsStream returns null
(26 answers)
Closed 6 years ago.
I have this peace of code to load a text file inside of a servlet:
String lFileName = mServletContext.getRealPath(mFile);
InputStream lInputStream = mServletContext.getResourceAsStream(lFileName);
InputStream lInputStream2 = mServletContext.getResourceAsStream(mFile);
Both InputStream's are null. I have absolutly no idear why.
The value of mFile is "file.txt".
The value of lFile is "C:\development\workspace\MyGwtApp\war\file.txt".
if I navigate with my explorer to that directory the file file.txt is in it...!
I test my gwt application with the super dev mode.
Compile the gwt app runs without problems.
Do you see the problem?
getResourceAsStream definition
Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader.
This means that you can read mFile if it exists in your classpath like under WEB-INF/classes. So place your file in your src directory where your java classes exists and look if the file comes to the classes directory and just use its name to get it as resource. Example: filename = "file.txt"
This question already has answers here:
Java: Get URI from FilePath
(5 answers)
Closed 7 years ago.
What is in Java the correct way to create a file URI for Windows? I tried
new URI("file", null, file.getAbsolutePath(), null);
but this complains about a relative path used in an absolute URI. I also tried prefixing the path with "//", but this makes 'c:' into a hostname. Then I prefixed the path with "////". A subsequent uri.getPath() then has a leading "//", which still does not seem right.
Is there any clean way to go from file.getAbsolutePath() to a URI with file:// protocol and back to a Windows path usable for new File(...) on Windows?
check File class docs. it provide toURI() method. below code seemed give output:
File file = new File("d:/myfolder/myfile.txt");
System.out.println(file.toURI());
This question already has answers here:
Rename a file using Java
(15 answers)
Closed 9 years ago.
I have file names like 323423233.
I want to add the last 2 digits of the file name and add it to the front and
make it 33/323423233 and add extension to it(like .doc).
What's a simple statement that I can use to achieve this?
This is 2013. This is Java 7. This is the time for Files and Path.
Base directory:
final Path baseDir = Paths.get("/path/to/baseDir");
Determine subdirectory for a file:
final String s = name.substring(name.length() - 2, name.length());
Create that directory:
final Path subDir = baseDir.resolve(s);
// Will not do anything if directory already exists...
// But will throw exception if unable to create
Files.createDirectories(subDir);
Write to the file:
final Path dst = subDir.resolve(name + ".doc");
Files.copy(src, dst);
Remove original:
Files.delete(src);
Or in one operation:
Files.move(src, dst);
This question already has answers here:
How to create a folder in Java?
(8 answers)
Closed 3 years ago.
I tried to use the File class to create an empty file in a directory like "C:/Temp/Emptyfile".
However, when I do that, it shows me an error : "already made folder Temp". Otherwise, it won't create one for me.
So, how do I literally create folders with java API?
Looks file you use the .mkdirs() method on a File object: http://www.roseindia.net/java/beginners/java-create-directory.shtml
// Create a directory; all non-existent ancestor directories are
// automatically created
success = (new File("../potentially/long/pathname/without/all/dirs")).mkdirs();
if (!success) {
// Directory creation failed
}
You can create folder using the following Java code:
File dir = new File("nameoffolder");
dir.mkdir();
By executing above you will have folder 'nameoffolder' in current folder.