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.
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.
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");
I am using Robotium to automate Android application. Created a android test project for the same. Inside project folder I want to keep some files like .properties/.xls etc. and want to read from /write to those files. For example, I have one config.properties file under my Android test project directory (src/main/java/config) and I want to access that file through coding:
For normal Java project I used following code snippet to load config.properties file:
CONFIG = new Properties();
FileInputStream fs = new FileInputStream(System.getProperty("user.dir")+"/src/main/java/config/config.properties");
CONFIG.load(fs);
This same code - when executed as Android JUnit project throwing error saying java.io.FileNotFoundException: //src/main/java/config/config.properties: open failed: ENOENT (No such file or directory) and unable to locate the file.
If anyone faced this anytime before please help me to get through.
Thanks
Sitam Jana
Software QA Enginner
Mindfire Solutions
You cannot access the internal structure of your java project that way. When you install your apk on the device, your java code gets converted to dex code for the Dalvik Virtual Machine, and you won't have the file structure from your project.
You can try the following:
If you only want to read the config.properties you should use the http://developer.android.com/reference/android/content/res/AssetManager.html class, and put your config.properties in /assets/ in your project.
If you also want to write your .properties file, consider creating a temporary file like this: Creating temporary files in Android.
Best Regards.
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.