How to read . txt file?

Asked

Viewed 1,390 times

5

How do I read a file .txt which is written in lines and check if in a row that file has a value if you have made a check on checkbox.

Example:

txt files:

123
234
2456

If you have the file 123, checks the checkbox 123, I don’t need that file txt be visible to the user only need the code to read it.

1 answer

3


To read a . txt from android, you can use the following code:

try {
    AssetManager assetManager = getResources().getAssets();
    InputStream inputStream = assetManager.open("nome-do-arquivo.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    String linha;
    LinkedList<String> linhas = new LinkedList<String>();
    while((linha = bufferedReader.readLine())!=null){
        //aqui com o valor da linha vc pode testar o que quiser, por exemplo: linha.equals("123")
        linhas.add(linha);
    }
    inputStream.close();
} catch (IOException e) {
    e.printStackTrace();
}
  • Alexandre, the night I will do the test and return here. .

  • 1

    Resolver Alexandre.

  • @Matheusarruda ok! Any questions, just let us know!

  • @Alexandrestrevenski in which folder the txt file should be?

  • @ramaral know that the answer is not yours, but you could tell me which folder should get the txt file.

  • @Rodolfo inside the 'Assets' folder'.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.