This question already has answers here:
Java in Eclipse: Where do I put files on the filesystem that I want to load using getResource? (e.g. images for an ImageIcon)
(2 answers)
Closed 8 years ago.
I'm developing a custom plugin for Eclipse and I have a powerpoint file named "extract.ppt" that I want to open through my source code.
When I place the file on my projects root diercotry and use:
File file = new File("/extract.ppt");
the file opens just fine.
But since im gonna be using a few more files on my application, I thought it would be a good idea to keep them all organised under a folder. So I created a folder named "files" under my main project folder and tried to use:
File file = new File("/files/extract.ppt");
but I get an error saying the file does not exist. I checked my Eclipse project's folder and the folder "files" as long as the "extract.ppt" are there.
Any ideas?
Use this instead:
File file = new File(".\\files\\extract.ppt");
The ".\\files\\..." is like saying LOOK on current directory IN folder files IN ...
You should use relative path:
File file = new File("./files/extract.ppt");
the files folder should be under the project.
Just like .setting or bin folder
Related
This question already has answers here:
Java, reading a file from current directory?
(8 answers)
FileNotFoundException even when the file is there
(3 answers)
Closed 9 days ago.
I'm not entirely sure if this is a Java issue or an IntelliJ issue, but I had a quick question about my File not being found via a path. My Main class is in the same directory as my input.txt file.
I though I should be able to do File file = new File("./input.txt"), except I get FileNotFoundException. When I do something like File file = new File("src/input.txt"), it does work.
I get that this may be a solution, but if I try to run this code outside of IntelliJ without a src directory, this will lead to an error where the File can't be found.
Is there any reason why I can't just do ./input.txt to specify that the File is in the same directory as the Main file?
That's because in IDEA, the working directory of your program is the root directory of your project, not src by default. Therefore, you can access the file by ./src/input.txt or src/input.txt, not input.txt. By the way, it's bad practice to put your resources in src, because it gets mixed with your source code, and it won't be included in the JAR file, which means you cannot access it when your project is packaged as a JAR file. If you are using maven, you can put input.txt inside the resources folder(src/main/resources), and access it by Main.class.getResource("/input.txt")(gives you a URL) or Main.class.getResourceAsStream("/input.txt")(gives you a InputStream) depending on your needs. So that the resources will always be available, no matter you are running the program in IDEA or from a JAR.
FileNotFoundException occurs when the file doesn't exist at the specified location.
File file = new File("./input.txt"), the current working directory (.) is being used as the file location.
If you are running the code from an IDE, the current working directory is usually the project root directory, not the directory where the code file is located.
So, if the input.txt file is located in the same directory as your code file, then you should use the relative path to it.
File file = new File("src/input.txt"), you are specifying the absolute path to the input.txt file, which is inside the src directory.
This is why it works, as the file is guaranteed to exist at that location.
If the file is located in the same directory as your code file, you can use a relative path, and if it's in a different directory, you should use an absolute path to it.
This question already has answers here:
Reading a resource file from within jar
(15 answers)
Closed 2 years ago.
I made a javafx project, when I finished I made a executable jar file. When I run it I get this error:
java.io.FileNotFoundException: src\Radiatorlista.txt
This is how my code looks like:
File radiatorok = new File("src/Radiatorlista.txt");
I know that this path will not work. I don't know how to specify the correct relative path.
This is how my jar file looks like.
The answer is that you need to put your txt files (or whatever you wan't to open) in the folder where your jar file is. For example my jar file is here:
F:/Example/for/my/file
You need to put your files in the file folder.
Then change your code like this:
File file = new File("Mytxt.txt");
After this your program should work.
I have a file with some data that I want only my application to have access to.
I was wondering in which folder I should put the file in my android project in order to have access to it using
File file = new File(context.getFilesDir(), filename);
Thanks
This is just an empty folder in Android device.
To access your files in assets
InputStream is = getAssets().open("subfolder/anyfile.txt");
to access files inside raw use
InputStream XmlFileInputStream = getResources().openRawResource(R.raw.somesrc);
getFilesDir() is just a folder that uses for openFileOutput(String, int) method. More detailed: https://stackoverflow.com/a/21230946/1979882
Use Assets Folder For files that will be bundled with the application and will be stored inside the apk as is. you can add an "asset" folder to your project by right clicking your app in the project explorer then select
New=> Folder=> Assets Folder.
you can open the files stored in the assets folder using the assets manager for example to open an image kept in /assets/img/image1.png:
InputStream is = assetManager.open("img/image1.png");
for HTML files you can load the file by its URL :
webView.loadUrl("file:///android_asset/"+name);
Here you can get a clear functionality of saving,retrieving a file
Here is the Link:http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
This question already has answers here:
How do I load a file from resource folder?
(21 answers)
Closed 6 years ago.
I have following project structure:
src
--Main.java
--resources
----Users.txt
And I am trying to create a file from Main.java like that:
File file = new File("resources/Users.txt");
However, I never succeed at doing so.
Why?
If it is a regular file outside a .jar, you are using a relative path. That means, the path to the file is formed from the path where you are calling the file from + the relative path. To make it work, you should invoke java within src folder
I want to acess class file from bin folder in android.
I was doing it using
File f = new File("/bin/filename.class");
Its working fine in java but in android giving path doesnt work, so
please suggest me other way to access class file of any java file in android.
You can't access file like
File f = new File("/bin/filename.class");
from bin folder. Android can't recognize this path.
Java .class files are converted to Dalvik Executable (.dex), in your apk there won't be any .class files, just one single dex file called classes.dex.
I think this post from Android Developers blog might help you: http://android-developers.blogspot.com/2011/07/custom-class-loading-in-dalvik.html