context.fileOpenInput(...) - file does not exist...but it does - java

I am probably doing something stupid here but my app cannot read data from the external storage. But i can write files just fine. In fact, i pull the files from an FTP server and write them to external storage which works fine. But as soon as I try to read them with:
getApplicationContext().openFileInput(files[i]);
I get java.io.FileNotFoundException: /data/data/co.za.infestation.opendesign.app/files/event1.txt: open failed: ENOENT (No such file or directory)
Which is weird because I know the files are there. And i know files[i] has the correct file name because I printed them before after:
String[] files = getExternalFilesDir(null).list();
Yes I did add the permissions in the manifest file... And yes, i did hardcode the file names without success..Maybe i need more coffee.
Thanks!

openFileInput()
is for opening files which reside in private internal memory. You can see the error path:
/data/data/co.za.infestation.............
Try to open with a
FileInputStream()
instead.

Related

How can I retrieve CSV file inside of the Unzip folder?

I am trying to read and parsed all the CSV files, these files are inside of extracted zip file, but unfortunately, I'm only getting this (No such file or directory) - How can I get inside that folder and read those files? Take note that this folder is already unzipped.
I'm also thinking, that if that is not possible - is there any way that I can extract the zip file to files only, for example below:
Zip File: ZipFolder.zip
Expected: (No folder at all, automatically the files is on the target location already).
CSVFile1.csv
CSVFile2.csv
Can someone help me to get through this? your help is appreaciated.. Thank you!

android - retrieve all files from /res/raw folder as File type

How can I retrieve all the files in /res/raw folder as File?
I have found this solution, and "New Folder" value I have replaced with "res/raw" as you can see in the following code:
public File[] getRawFiles() {
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "res/raw");
return yourDir.listFiles();
}
When program is started, I get an exception on line return yourDir.listFiles:
Caused by: java.lang.NullPointerException: Attempt to get length of null array
How can I fix this, and what is correct path to "res/raw/" ?
How can I retrieve all the files in /res/raw folder as File?
You cannot do this. As we discussed yesterday, resources are not files on the filesystem of the device. They are files on your development machine. They are merely entries in an APK file on the device.
Either:
Do whatever you are trying to do some other way that does not involve files, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of a resource to a local file, such as on internal storage, and using reflection to iterate over the actual resources, or
Switch to using assets/ instead of res/raw/, as AssetManager allows you to list() assets (though you still only get an InputStream on an asset, as like resources, assets are not files on the device)

ENOENT (No such file or directory) on database file

java.io.IOException: open failed: ENOENT (No such file or directory)
This happens on this line of code:
File db = getDatabasePath("questions");
db.createNewFile();//HERE
I am trying to create a database file with the name questions.db , which will be empty. I will then pull the file from the server and insert it there. I then want to be able to access the database from sqlite.
You did not show the stack trace, it can help to pinpoint it. But since the File#createNewFile() is not doing much, the only possible reason for this to fail is, that the (relative) path includes a non-existing parent directory. createNewFile() will not create those.
You typically write
File db = ...
db.getParentFile().mkdirs();
db.createNewFile();
to fix this. (Or use a different - aka existing - location).

read a file from within app folder in android

I want to read a file that is saved in my java folder (same where my activities are) in package com.example so it's java-->com.example--> data.ser. However I'm getting java.io.FileNotFoundException: /java/com/example/data.ser: open failed: ENOENT (No such file or directory) when I call
ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("java/com/example/data.ser")));. I tried removing the java part or just leaving the file name on its own but nothing works. I am not loading it from external/internal storage but just from within the app. How to do it? I'm using android studio. Thanks for help
Files has to be stored on 'assets' or 'raw' folder, depending on what kind of file you are going to store. On 'java' you should set code class only. You can find an easy example here:
raw file example

RandomAccessFile to read files in a Jar file

I am having problem to use JWNL wordnet in a Jar file.
JWNL uses RandomAccessFile to read wordnet dictionary files. In order to create a Jar file, wordnet dictionary files are put in resources/wordnet folder. As resources is in my Build Path, I have no problem to run the application I created in Eclipse. However, when I use another application to run the created jar file, I get the following error:
java.io.FileNotFoundException: resources/wordnet/data.noun (No such file or directory)
from the following code:
RandomAccess _file = new RandomAccessFile(path, _permissions);
I use the following code to check the current working directory:
URL location = PrincetonRandomAccessDictionaryFile.class.getProtectionDomain().getCodeSource().getLocation();
System.out.println(location.getFile());
It seems both situation have the same location: /project/bin/
How should I fix the problem? Thank you
The key information you seem to be missing is that Jar files are compressed, and you can't "seek" because of the compression (which is I believe the DEFLATE algorithm).
However, you could extract the file(s) into temp file(s) on start and then use that. Temp files would be removed on application exit, and are the best answer I can think of.
RandomAccessFile to read files in a Jar file
There are no files in a JAR file. There are JAR entries. You can't read them with FileInputStreams, RandomAccessFiles, or FileReaders.You need to use a JarInputStream or its friends.

Categories

Resources