Obtain path to file with filename [duplicate] - java

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();

Related

How to open a file in the same directory as the .jar file of the application? [duplicate]

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");

Filenotfoundexception JAVA, need full path [duplicate]

This question already has answers here:
java.io.FileNotFoundException: the system cannot find the file specified
(8 answers)
Closed 4 years ago.
My program get a filename in parameter but if i want to use it i need the full directory path or i get filenotfoundexception.
For example:
My program got sample.txt in parameter from C:\Users\me\documents.
File file = new File(args[0]);
FileReader fr = new FileReader(file);
That throw filenotfoundexception.
So what should i use to locate the file?
I saw so many similar question but i didnt find solution :(
I tried to use getResources and getPath but nothing.
You can open a file with just the filename if that file exists in the same directory as of your source code. If the file is located at any random location then you need to give the complete path of the file along with its name.
Eg: c:\documents\sample.txt
Or another thing that you can try is recursively going through all folders present in your file system and locate the file. However, this is will be a very horrible solution.
File fileName = new File("myfile.txt");
if(!fileName.exists()) {
fileName.createNewFile();
}
FileOutputStream oFile = new FileOutputStream(fileName, false);
add this code if your file is not on the location this will create a one for you then you wont be getting filenotfound exception at the latter part

Accesing resources in a JAR [duplicate]

This question already has answers here:
How do I read a resource file from a Java jar file?
(9 answers)
Closed 8 years ago.
OK guys, so I'm trying to compile my game into a jar file, but I can't get the loading of images to work. When run from NetBeans, all is fine. But in the JAR, the URL is always null.
Here is the code I'm using:
URL url = this.getClass().getResource("/textures/Lava.jpg");
BufferedImage sourceImage = null;
try
{
sourceImage = ImageIO.read(url);
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
I have tried unziping the JAR and checking the contents, my textures folder is there and the images inside also. Any ideas what I'm doing wrong?
Previously answered here:
Accessing a file inside a .jar file
Better use
new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/resources/" + filename)))
you should get the URL path as relative path of your system.

Problem with Java FileReader [duplicate]

This question already has answers here:
FileNotFoundException, the file exists Java [closed]
(2 answers)
Closed 7 years ago.
Hello I have this in my code
File file = new File("words.txt");
Scanner scanFile = new Scanner(new FileReader(file));
ArrayList<String> words = new ArrayList<String>();
String theWord;
while (scanFile.hasNext()){
theWord = scanFile.next();
words.add(theWord);
}
But for some reason I am getting a
java.io.FileNotFoundException
I have the words.txt file in the same folder as all of my .java files
What am I doing wrong? Thanks!
Tip: add this line to your code...
System.out.println(file.getAbsolutePath());
Then compare that path with where your file actually is. The problem should be immediately obvious.
The file should reside in the directory from which you execute the application, i.e. the working directory.
Generally it's a good idea to package data files with your code, but then using java.io.File to read them is a problem, as it's hard to find them. The solution is to use the getResource() methods in java.lang.ClassLoader to open a stream to a file. That way the ClassLoader looks for your files in the location where your code is stored, wherever that may be.
try:
URL url = this.getClass().getResource( "words.txt" );
File file = new File(url.getPath());
You haven't specified an absolute path. The path would therefore be treated as a path, relative to the current working directory of the process. Usually this is the directory from where you've launched the Main-Class.
If you're unsure about the location of the working directory, you can print it out using the following snippet:
System.out.println(System.getProperty("user.dir"));
Fixing the problem will require adding the necessary directories in the original path, to locate the file.

How to create empty folder in java? [duplicate]

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.

Categories