26

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 returned a string like:

"something written\nin this file\nis to be read by\nthe InputStream"

what I thought of is(Pseudocode):

make an inputstream
is = getAssest().open("textfile.txt");  //in try and catch
for loop{
string = is.read() and if it equals "." (i.e. from 1., 2., 3. etc) add "/n" ...
}
Positive Navid
  • 2,481
  • 2
  • 27
  • 41
RE60K
  • 621
  • 1
  • 7
  • 26

3 Answers3

36

try this

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.*;

public class FileDemo1 extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            playWithRawFiles();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), "Problems: " + e.getMessage(), 1).show();
        }
    }

    public void playWithRawFiles() throws IOException {      
        String str = "";
        StringBuffer buf = new StringBuffer();            
        InputStream is = this.getResources().openRawResource(R.drawable.my_base_data);
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            if (is != null) {                            
                while ((str = reader.readLine()) != null) {    
                    buf.append(str + "\n" );
                }                
            }
        } finally {
            try { is.close(); } catch (Throwable ignore) {}
        }
        Toast.makeText(getBaseContext(), buf.toString(), Toast.LENGTH_LONG).show();
    }
}
emazzotta
  • 1,879
  • 3
  • 20
  • 31
Yogesh Tatwal
  • 2,722
  • 20
  • 43
14

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
    } 

}
Vinay
  • 6,891
  • 4
  • 32
  • 50
1
File fe = new File("abc.txt");
FileInputStream fis = new FileInputStream(fe);
byte data[] = new byte[fis.available()];
fis.read(data);
fis.close();
String str = new String(data);
System.out.println(str);
zb226
  • 9,586
  • 6
  • 49
  • 79
Ziyad
  • 11
  • 3
  • It would help to provide context to how and where to use your code – Adonis Aug 23 '17 at 08:21
  • if you have any txt file and u want to read that text file using FileInputStream you can do so and.......... in your File fe=new File(abc.txt); and this will be display in your console read data String str=new String(data); System.out.println(str); – Ziyad Aug 23 '17 at 08:45