Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Moritz Gruber
12,073 PointsHow to read from a text File, which is in the same folder as the java file in an complex andriod studio?
Hey, I want to upgrade my FunFacts app. Each fact should be red out of a text file called test.txt, instead of using a simple string array.
Without AdroidStudio i got no trouble with this.
In my normal IntelliJ Idea, this code works:
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
String zeile = br.readLine();
.....
my project folder looks like the same in Android Studio, but it wont work there. (i got my test.ext file next to the ReadLine.java file, which calls the code above)
I would really appreciate your help Sincerely Moritz
2 Answers
Seth Kroger
56,416 PointsWhat you need is to create an "assets" directory in "app/src/main" (at the same level as java and res) and place the text file there. Then you can open the file with Android's AssetManager class.
AssetManager assetManager = getAssets();
InputStream = assetManager.open("text.txt");
Moritz Gruber
12,073 PointsEven if its 9 days ago, your answer helped me that day a lot and i fixed my issues. Thank you, Seth for your help!