Where to save a file to be accessable via FileInputStream - java

I need a FileInputStream from a file but I don't know where to place the file.
I wrote this code:
InputStream is = new FileInputStream("src/test/resources/Key.p12");
But, where should I save file Key.p12?

InputStream is = new FileInputStream("src/test/resources/Key.p12");
There is no src/test/resources/ directory on any Android device.
Yes, but in app/src/test, there doesnt exist a dir called "resources". Only "res". Maybe it works when i create a dir called "resources"...
No, it will not. FileInputStream will only work for things that are files on the device. Anything packaged in your APK will not be files on the device.
Instead, store this file in app/src/main/assets/. Then, use AssetManager and open() to get an InputStream on that asset.

where ever you want to put the file
just copy the destination and paste it in the constructor

Related

Why is android unable of finding my file?

I want to access a file in included in the folder of the app and I want to be able to access it localy. I also don't want to use methods like android.getresources() and just use
File file = new File( filePath );
My file is saved in app/src/main/assets/ball.obj
The problem is when I do File file = new File("app/src/main/assets/ball.obj");, it alwas gives me a FileNotFoundException. I also tried moving the file in other places and absolute pathes, but it still doesn't work. Please help.
My file is saved in app/src/main/assets/ball.obj
That is a relative path to a file on your development machine. Assets are not files on the filesystem of the device. You cannot use File to access an asset.
To get an InputStream on your asset, call getAssets() on your Activity or other Context, then call open() on the AssetManager returned by getAssets().
For your call to open(), you need to pass in the relative path within assets/ to the asset that you wish to read in. So, if you are in a method of your Activity, getAssets().open("ball.obj") will give you an InputStream that you can use to read in the contents of that particular asset.

FileInputStream Javafx in .jar files

I am creating a Javafx application in Intelij and FileInputStream works perfectly. However, when I create a .jar file from the project and try to run it the code fails to run as it is unable to locate the file in the file input stream.
Here is my code:
ObjectInputStream os = new ObjectInputStream(new FileInputStream("src/settingStorage.bin"));
Am I doing something wrong?
There is always some issue accessing a file outside a jar, depending on where the file location is. You can check this SO question/answer to have an idea on accessing your file.
Read properties file outside JAR file
Try this:
EDIT: Look at this again, I left part out.
ObjectInputStream os = new ObjectInputStream(new FileInputStream(SomeClass.class.getResourceAsStream(“settingStorage.bin")));
This usually works for me. When I run this, it operates fine, in AND out of the development area. Don’t include /src, as when you call getResourceAsStream off a class, it already checks inside the jar.
Cheers!

inputStream gives java.io.FileNotFoundException

I have created new thread in my android app to read .p12 file like this:
AssetManager am = getAssets();
InputStream inputStream = am.open("C:\\Users\\UkrBikeApp-ff55878cb577.p12");
This file exist in directory. But every time I get
java.io.FileNotFoundException
The simular code works in simple java app without any problems. What is the reason?
First, C:\\Users\\UkrBikeApp-ff55878cb577.p12 is a path to a file on a Windows machine. Android is not Windows.
Second, C:\\Users\\UkrBikeApp-ff55878cb577.p12 is a filesystem path. It is not a path to an asset within your APK, and so AssetManager would not know what to do with it anyway.
If you want to package this file in your app:
Create an assets/ directory in the appropriate module and source set (e.g., app/src/main/assets/)
Put the file in assets/
Use open() on AssetManager with the plain filename

File doesn't read after creating jar using netbeans

i have a small application which checks for values from a file and display the result in a jframe.
A file contain list of word to check. this file is placed in project folder "testing" and the main source testing.java file is present in location "testing\src\testing"
input file : c:\document..\netbeans\testing\
java file : c:\document..\netbeans\testing\src\testing\
when i place the input file inside folder "c:\document..\netbeans\testing\src\testing\
" the input file is not taken as input, it works only when kept on folder "c:\document..\netbeans\testing\"
so when a jar file is created it has not included the input file in that, even i manually input that is not taking the input file in and working.
some path setting issue? what can be done to solve this issue?
any help pls??
Once you create the jar, the file becomes an embedded resource. If you try to read it as a File it will no long be the same file system path as you originally use in the program. It must now be read from the class path.
To read the file from the class path, you will want to use getClass().getResourceAsStream(), which return an InputStream. If your file is in the same location (package) as your class file, then you should use
InputStream is = getClass().getResourceAsStream("input.txt");
Then you can read from the InputStream
BufferedReader reader = new BufferedReader (new InputStreamReader(is));
This generally happens, when you don't use absolute path...!
As when you run your program from IDE(Netbeans) then the HOME_FOLDER is your ProjectFolder. Relative to which you would have given the file_path(that has to be accessed in your program).
But after building, jar is present in ProjectFolder/dist. When you run the jar file the HomeFolder is not ProjectFolder rather it is ProjectFolder/dist.
So, to make it successful, to need to copy all files and folders from ProjectFolder/dist to ProjectFolder.
Then run the jar.. Hope it will fix the issue
Try putting double backslashes in your file paths. Like this:
c:\\document..\\netbeans\\testing\\src\\testing\\
This is the format that java normally requires it to be in

Java - read file from directory for jar

I have an application that creates a temporary mp3-file and puts it in a directory like C:\
File tempfile = File.createTempFile("something", ".mp3", new File("C:\\));
I'm able to read it by just using that same tempfile again.
Everything works fine in the Eclipse IDE.
But when I export my project for as a Runnable jar, my files are still being made correctly (I can play them with some normal music player like iTunes) but I can't seem to read them anymore in my application.
I found out that I need to use something like getClass().getResource("/relative/path/in/jar.mp3") for using resource files that are in the jar. But this doesn't seem to work if I want to select a file from a certain location in my file system like C:\something.mp3
Can somebody help me on this one?
It seems you dont have file name of the temp files . When you was running your program in eclipse that instance was creating a processing files, but after you made a runable you are not able to read those file that instance in eclipse created, You runable file can create its own temp file and can process them,
To make temp files globe put there (path + name ) entries in some db or property file
For example of you will create a temp file from the blow code
File tempfile = File.createTempFile("out", ".txt", new File("D:\\"));
FileWriter fstream = new FileWriter(tempfile);//write in file
out = new BufferedWriter(fstream);
the out will not be out.txt file it will be
out6654748541383250156.txt // it mean a randum number will be append with file
and you code in runable jar is no able to find these temp files
getClass().getResource() only reads resources that are on your classpath. The path that is passed to getResource() is, in fact, a path relative to any paths on your current classpath. This sounds a bit confusing, so I'll give an example:
If your classpath includes a directory C:\development\resources, you would be able to load any file under this directory using getResource(). For example, there is a file C:\development\resources\mp3\song.mp3. You could load this file by calling
getClass().getResource("mp3/song.mp3");
Bottom line: if you want to read files using getResource(), you will need those files to be on your classpath.
For loading from both privileged JARs and the file system, I have had to use two different mechanisms:
getClass().getClassLoader().getResource(path), and if that returns null,
new File(path).toURI().toURL();
You could turn this into a ResourceResolver strategy that uses the classpath method and one or more file methods (perhaps using different base paths).

Categories

Resources