How to read .txt file?

5

How do I read a .txt file that is written in rows and check if a line in that file has a value if you make a check in checkbox .

Example:

Files txt:

123
234
2456

If you have the file 123 , check the checkbox 123 , I do not need the file txt to be visible to the user I just need the code to read it.

    
asked by anonymous 05.09.2015 / 19:10

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();
}
    
07.09.2015 / 15:30