Reading text file line by line in android

Hi, I just started to study the development of Android, and I'm trying to create an application that reads text from files. I searched all over the internet, but I didn't seem to find a way to do this, so I have a few questions.

1. How to do it? what is the preferred way to read a file line by line in android?

2. Where should I store the file? should it be in the raw folder or perhaps in the resource folder?

here is what I already tried: "(I think the problem may be finding the file ..)

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.filereader); try { // open the file for reading InputStream fis = new FileInputStream("text.txt"); // if file the available for reading if (fis != null) { // prepare the file for reading InputStreamReader chapterReader = new InputStreamReader(fis); BufferedReader buffreader = new BufferedReader(chapterReader); String line; // read every line of the file into the line-variable, on line at the time do { line = buffreader.readLine(); // do something with the line System.out.println(line); } while (line != null); } } catch (Exception e) { // print stack trace. } finally { // close the file. try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } 

}

+6
source share
2 answers

Depending on what you are going to do with this file. If your goal is to only read the file, then the resource folder is the way to go. If you want to save information in this file when you work with it, you must place it on the device.

If you choose option number 2, you need to decide if you want other applications to read the file. More information can be found at this address:

http://developer.android.com/training/basics/data-storage/files.html

In addition, you can read / write directly to the device with the standard java procedure, as described above. Although, the file path is likely to be

"/SDCard/text.txt"

EDIT:

Here is a code snippet to get started with

 FileInputStream is; BufferedReader reader; final File file = new File("/sdcard/text.txt"); if (file.exists()) { is = new FileInputStream(file); reader = new BufferedReader(new InputStreamReader(is)); String line = reader.readLine(); while(line != null){ Log.d("StackOverflow", line); line = reader.readLine(); } } 

But it assumes that you know that you put text.txt at the root of your SD card.

If the file is located in the resource folder, you should do this:

 BufferedReader reader; try{ final InputStream file = getAssets().open("text.txt"); reader = new BufferedReader(new InputStreamReader(file)); String line = reader.readLine(); while(line != null){ Log.d("StackOverflow", line); line = reader.readLine(); } } catch(IOException ioe){ ioe.printStackTrace(); } 
+24
source

However, your code looks good, you should make your file asynchronously read. For the file path, it depends on whether it is a file that you link in the APK, or with a file that you upload to the application data folder. Depending on what version of android you are planning, I would use try with resources ...

to read from assets, you can do this in your action:

 reader = new BufferedReader( new InputStreamReader(getAssets().open("filename.txt"))); 
+1
source

Source: https://habr.com/ru/post/971020/


All Articles