簡體   English   中英

資產中讀取文件的示例

[英]example of read file from assets

我正在研究從資產中獲取應用程序讀取文件的示例,但它不起作用,(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);
             }

     });

你能幫我嗎?

用這個

    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]'

看看這個答案和評論

在這個例子中,我正在從/ 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();


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM