Reading a simple text file

前端 未结 7 2001
甜味超标
甜味超标 2020-11-22 14:10

I am trying to read a simple text file in my sample Android Application. I am using the below written code for reading the simple text file.

InputStream inpu         


        
7条回答
  •  庸人自扰
    2020-11-22 14:48

    try this,

    package example.txtRead;
    
    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.StringTokenizer;
    import java.util.Vector;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class txtRead extends Activity {
        String labels="caption";
        String text="";
        String[] s;
        private Vector wordss;
        int j=0;
        private StringTokenizer tokenizer;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            wordss = new Vector();
            TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
            helloTxt.setText(readTxt());
     }
    
        private String readTxt(){
    
         InputStream inputStream = getResources().openRawResource(R.raw.toc);
    //     InputStream inputStream = getResources().openRawResource(R.raw.internals);
         System.out.println(inputStream);
         ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    
         int i;
      try {
       i = inputStream.read();
       while (i != -1)
          {
           byteArrayOutputStream.write(i);
           i = inputStream.read();
          }
          inputStream.close();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
    
         return byteArrayOutputStream.toString();
        }
    }
    

提交回复
热议问题