阅读使用InputStream的一个文本文件 [英] Reading a textfile using InputStream

查看:106
本文介绍了阅读使用InputStream的一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能读取文本文件就像在Android应用:

 1.something写
2.in这个文件
3.is要由读
4.为InputStream
......

这样我就可以返回的字符串一样:

 的东西写/ nin的这个文件/ NIS由/ N / n此InputStream中读取

我的想法是(伪code):

 做一个InputStream
是= getAssest()开(TextFile.txt的); //在try和catch
循环{
串= is.​​read(),并且如果它等于。 (即从1,2,3等)添加/ N...
}


解决方案

试试这个

 进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.Toast;
进口java.io. *;公共类FileDemo1延伸活动{    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
        尝试{
            playWithRawFiles();
        }赶上(IOException异常五){
            Toast.makeText(getApplicationContext(),问题:+ e.getMessage(),1).show();
        }
    }    公共无效playWithRawFiles()抛出IOException
        字符串str =;
        StringBuffer的BUF =新的StringBuffer();
        InputStream为= this.getResources()openRawResource(R.drawable.my_base_data)。
        尝试{
            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是));
            如果(是!= NULL){
                而((海峡= reader.readLine())!= NULL){
                    buf.append(STR +\\ n);
                }
            }
        } {最后
            尝试{is.close(); }赶上(Throwable的忽略){}
        }
        Toast.makeText(getBaseContext(),buf.toString(),Toast.LENGTH_LONG).show();
    }
}

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

这篇关于阅读使用InputStream的一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆