简体   繁体   English

使用InputStream读取文本文件

[英]Reading a textfile using InputStream

How can I read a text file like in android app: 如何读取Android应用程序中的文本文件:

"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" ...
}

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();
    }
}

Use BufferedReader to read the input stream. 使用BufferedReader读取输入流。 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. 由于BufferedReader将从字符输入流中读取文本,因此缓冲字符以便有效读取字符,数组和行。 InputStream represents an input stream of bytes. InputStream表示输入的字节流。 reader.readLine() will read the file line by line. reader.readLine()reader.readLine()读取文件。

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
    } 

}
                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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM