Reading a textfile using InputStream

前端 未结 3 1146
耶瑟儿~
耶瑟儿~ 2020-12-31 01:04

How can I read a text file like in android app:

\"1.something written
2.in this file
3.is to be read by
4.the InputStream
...\"

so I can be

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-31 01:18

    Use BufferedReader to read the input stream. As BufferedReader will read text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. InputStream represents an input stream of bytes. reader.readLine() will read the file line by line.

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    StringBuilder out = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        out.append(line);   // add everything to StringBuilder 
        // here you can have your logic of comparison.
        if(line.toString().equals(".")) {
            // do something
        } 
    
    }
    

提交回复
热议问题