getting input from file in an android project - java

I am trying to make a dictionary in android.
But i am facing problem to get input from text file.
I have put the text file in res/raw directory.
and write the code:
InputStream inputStream=getResources().openRawResource(R.raw.Dictionary);
but having exception my IDE indicates a problem under raw word.
How can I fix this ?

Place the file in the assets folder and open it like this
getResources().getAssets().open("dictionary.txt");

Related

can I use .txt files in JAVA using VS code?

I am trying to access the text file in java and the code I have works in other IDEs but in VS code it keeps saying File Not Found - (produced from my exception handling)
I can't find anything about how to access text files with VScode. Is this possible please and does anyone know how?
I have tried to create the file in VScode with the new text file option and also by loading a pre-existing file and get the same result each time
Place the .txt file in the project root directory so that the code reads to the file location. As shown below
If the file is not with the project, use absolute paths in the code.

How to read a file in android studio project folder?

I am new to android and it seems I cannot get a filepath of a file within the project structure.
I have a text file "db-info.txt", that I am trying to read from a class. Every method I tried has said no file found. This file I am trying to read from a java class within the same folder.
Any help is appreciated. Here is where my file is within the project -

File not found on Android Device

I have an error here:
FileHandle file = Gdx.files.internal("hscore.json");
Showing that it can't be found:
...
com.badlogic.gdx.utils.GdxRuntimeException: File not found: hscore.json (Local)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
...
It works on my Desktop, but fails like this on my Android Phone.
I use Android Studio, where I keep the file in the assets folder which is in the android folder.
I have no idea why it can't find it on the android?
Update: I don't know what happened in meantime, but now it highlights this line of code after throwing the same error:
String s = file.readString();
It can read .txt files and other files I'm using just fine, seems the .json file is the problem for android?
Until this is resolved, I'm using Preferences to save data there, rather than the whole class in .json
Thanks to a chat with p.streef This question has come to a closure, sorta.
1) I created a new test.txt file and it worked fine reading that.
Maybe then the .json file itself was a real issue?
2) But then I renamed it to text.json, and afterwards back to hscore.json
It worked fine in both cases when opening and reading the file.
I had no idea why the initial file .json was not readable?
3) I also killed and restarted my android studio in meantime because it froze at a point when building my project.
After all of this the problem of file not being found was resolved by itself.
Anyway then I realized that the file is not writable since internal files are read-only.
I decided to stick then with the Preferences as the final solution.

Write in a txt file located in /raw folder

here's my problem.
I have a txt file in the /raw folder and I want to write into it.
From the Android guide i saw this:
Tip: If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
So in my code I did this:
FileOutputStream myFile = openRawResource(R.raw.max_easy, Context.MODE_PRIVATE);
But gives me error in openRawResource()... How do i solve this?
Thank you for the help!
I have a txt file in the /raw folder and I want to write into it
That is not possible at runtime. Resources and assets are read-only at runtime.
But gives me error in openRawResource()... How do i solve this?
openRawResource() is for reading in the resource, and it gives you an InputStream. You are welcome to write your data to an ordinary file, such as on internal storage.

Android FileNotFound Exception with a plist file that will be built in

I am trying to open a file for an Android app I am making. I tested the code that opens this file on a regular Java project, and it opens fine. However when I use this code in an Android Java project I get an java.io.FileNotFoundException error.
File file = new File ("list.plist");
Now the list.plist file is in the parent directory of the project. This file will be included in the app as its the only one it will be using.
I guess I am used to Xcode where I can just place the file anywhere in the project and I can access it without a problem.
How am I supposed to structure this path?
Thanks :-)
I've used this but it also did not work.
File file = new File(context.getFilesDir().getAbsoluteFile(), "list.plist");
Firstly, you need to ensure the file you are using is packaged within your app - such as in the assets directory.
Secondly, Android automatically compresses plist files based on the extension. As a workaround, change the extension to something Android will not compress. These extensions include:
".jpg", ".jpeg", ".png", ".gif",
".wav", ".mp2", ".mp3", ".ogg", ".aac",
".mpg", ".mpeg", ".mid", ".midi", ".smf", ".jet",
".rtttl", ".imy", ".xmf", ".mp4", ".m4a",
".m4v", ".3gp", ".3gpp", ".3g2", ".3gpp2",
".amr", ".awb", ".wma", ".wmv"
(for full article, see dealing-with-asset-compression-in-android-apps)
You need to put this file in the assets dir and not in the parent folder. Any files in the parent folder need not be packaged with the app. Moreover, the structure of your files on a phone is not the same as you see in eclipse.
You should use "res/raw" or "assets" directory to include a raw file in your Android project.
see: http://developer.android.com/guide/topics/data/data-storage.html
I added the list.plist to the assets folder and renamed it list.mp3 to avoid compression issues.
AssetManager assetManager = getAssets();
InputStream inputStream = assetManager.open("list.mp3");
This successfully opened the file without problems, which now allows me to parse the inputStream.
Thanks to Phil for giving sending me in the right direction.

Categories