0

I am a beginner in coding . my app is just a book , it contains some layouts and text files , I know how to write the code to switch to another layout , but don't know the code to open the text files , and where to store the text file in the app's folders . thanks .

3 Answers3

0

Do you store your textfiles in the project or do you create a folder once the app is installed and want to read the files from there?

For the first case it would be much easier to store the text of the files in the string.xml file and get them with the "getResources()"-methode.

For the second case you need to have the URI of the file and read the file with bufferedReader.

Best, Sebi

sebi0920
  • 300
  • 4
  • 12
0

You should start with learning first from official developer bloges ,After as per your question you can put your text file inside assets folder . To read the file, you can use a Scanner. If your Strings don't have spaces in them, this would work:

Scanner in = new Scanner(new File("assets/foo.txt");

in.next();      // Read the next String
in.nextInt();   // Read the next int
in.nextFloat(); // Read the next float

If you have spaces in your Strings, you should use commas and tell the Scanner to use ,s as a delimiter:

Scanner in = ...;
in.useDelimiter(",");
Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46
0

To open stored text files, I will recommend you store the text files in either raw or asset folder, use AssetManager to open the text files in asset or just read from the raw directory. Follow this tutorial for details on assets manager.

the assets folder is directly under your menu folder same level as your java and res folder http://www.technotalkative.com/android-read-file-from-assets/

and for raw use this tutorial

raw folder is in the resource folder http://android-er.blogspot.com.ng/2010/07/display-text-file-in-resraw_01.html

Aliyu Abdullahi
  • 129
  • 1
  • 4