资产中读取文件的示例

时间:2017-02-27 10:04:59

标签: android

我正在制作一个示例,让应用程序从资源中读取文件,但它不起作用,(file.txt)中的文本不会出现。

代码:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_read_file);

    b_read= (Button)findViewById(R.id.b_read);
    tv_text= (TextView) findViewById(R.id.tv_text);

     b_read.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             String text="";

             try{
                 InputStream is = getAssets().open("file.txt");
                 int size = is.available();
                 byte[] buffer = new byte[size];
                 is.read(buffer);
                 is.close();
                 text = new String(buffer);


             }catch (IOException ex) {
                 ex.printStackTrace();
             }
             tv_text.setText(text);
             }

     });

你能帮忙吗?

2 个答案:

答案 0 :(得分:0)

使用此

    StringBuilder DataString = new StringBuilder();
    InputStream fIn = null;
    InputStreamReader isr = null;
    BufferedReader input = null;
    try {
        fIn = getAssets()
                .open("file.txt", Context.MODE_WORLD_READABLE);
        isr = new InputStreamReader(fIn);
        input = new BufferedReader(isr);
        String line = "";
        while ((line = input.readLine()) != null) {
            DataString.append(line);
        }
    } catch (Exception e) {
        e.getMessage();
    } finally {
        try {
            if (isr != null)
                isr.close();
            if (fIn != null)
                fIn.close();
            if (input != null)
                input.close();
        } catch (Exception e2) {
            e2.getMessage();
        }
    }

    tv_text.setText(DataString.toString());

即使你的方法是正确的,它会多次返回'android.content.res.AssetManager $ AssetInputStream @ [code]'

查看this 回答和评论

答案 1 :(得分:0)

在这个例子中,我正在从/ assets文件夹中读取tests.json:

public class Constants {
    public static final String BASE_DIR = "arqospocket";
    public static final String CONFIG_DIR = "cfg";
    public static final String TESTS_DIR = "tests";
    public static final String TESTS_FILE = "tests.json";
}
    private String getJsonTestList() {

    String path = Environment.getExternalStorageDirectory().getAbsolutePath()
            + File.separator
            + BASE_DIR
            + File.separator
            + TESTS_DIR;
    final File file = new File(path, TESTS_FILE);
    if(file.exists()){
        Log.d(TAG, "getJsonTestList :: Reading tests from external file: " + file.getAbsolutePath());
        StringBuilder text = new StringBuilder();

        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line = null;
            while ((line = br.readLine()) != null) {
                text.append(line);
            }

            br.close();
            return text.toString();
        } catch (IOException e) {
            Log.d(TAG, "getJsonTestList :: Exception reading from external file: " + e.getMessage());
        }
    }
 //TODO remove this
    Log.d(TAG, "getJsonTestList :: Reading tests from asset manager" );

    AssetManager assetManager = getAssets();
    ByteArrayOutputStream outputStream = null;
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open("tests.json");
        outputStream = new ByteArrayOutputStream();
        byte buf[] = new byte[8192];
        int len;
        try {
            while ((len = inputStream.read(buf)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
        }
    } catch (IOException e) {
    }
    return outputStream.toString();


}