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.
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:
Read properties file outside JAR file
(8 answers)
How to get the path of a running JAR file?
(33 answers)
Closed 6 years ago.
I have a folder structured like this:
MyFolder:
file1.xml
file2.xml
project.jar
But if in a class I use:
File f = new File("file1.xml");
I receive an error, because it doesnt find the file. Why?
You should use a relative path in your code.
Example: File f = new File("./file1.xml");
If you are using Windows the code you posted will work, but not on Linux where the default parent file is your home.
But you can do in any OS by using:
public class MyClass {
public void loadFile() {
URL url = MyClass.class.getProtectionDomain().getCodeSource().getLocation();
File jar = new File(url.toURI());
File f = new File(jar.getParent(), "file1.xml");
//your code
}
}
PS: This needs to be inside project.jar because you are getting the location where you jar file is.
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:
Get path of Android resource
(3 answers)
How can I write a Drawable resource to a File?
(2 answers)
Closed 8 years ago.
I am writing an Android app and I need to do something I would consider incredibly simple, yet am having the hardest time figuring out.
How can I obtain the path to a JPG file so that I can upload it to a server?
The file is snoopy.jpg and is in the res/drawable folder
I've tried:
File sourceFile = new File("android.resource://com.appname.something/drawable/snoopy");
But, that's not a file
I've tried:
File sourceFile = new File("drawable/snoopy");
and that doesn't work.
I tried putting snoopy.jpg in the root of the app directory and attempting:
File sourceFile = new File("/snoopy.jpg");
And that still didn't work.
Any help would be greatly appreciated!
Try this :
InputStream is = getResources().openRawResource(R.drawable.snoopy);
You can open an InputStream from your drawable resource using the above code. Also, before doing any file operation, you need to check if file.exist() and if it returns false then you need to create a file via f.createNewFile();
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);