Reading a simple text file

前端 未结 7 1989
甜味超标
甜味超标 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:34

    Having a file in your assets folder requires you to use this piece of code in order to get files from the assets folder:

    yourContext.getAssets().open("test.txt");
    

    In this example, getAssets() returns an AssetManager instance and then you're free to use whatever method you want from the AssetManager API.

    0 讨论(0)
  • 2020-11-22 14:34

    To read the file saved in assets folder

    public static String readFromFile(Context context, String file) {
            try {
                InputStream is = context.getAssets().open(file);
                int size = is.available();
                byte buffer[] = new byte[size];
                is.read(buffer);
                is.close();
                return new String(buffer);
            } catch (Exception e) {
                e.printStackTrace();
                return "" ;
            }
        }
    
    0 讨论(0)
  • 2020-11-22 14:37

    In Mono For Android....

    try
    {
        System.IO.Stream StrIn = this.Assets.Open("MyMessage.txt");
        string Content = string.Empty;
        using (System.IO.StreamReader StrRead = new System.IO.StreamReader(StrIn))
        {
          try
          {
                Content = StrRead.ReadToEnd();
                StrRead.Close();
          }  
          catch (Exception ex) { csFunciones.MostarMsg(this, ex.Message); }
          }
              StrIn.Close();
              StrIn = null;
    }
    catch (Exception ex) { csFunciones.MostarMsg(this, ex.Message); }
    
    0 讨论(0)
  • 2020-11-22 14:40

    This is how I do it:

    public static String readFromAssets(Context context, String filename) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(context.getAssets().open(filename)));
    
        // do reading, usually loop until end of file reading  
        StringBuilder sb = new StringBuilder();
        String mLine = reader.readLine();
        while (mLine != null) {
            sb.append(mLine); // process line
            mLine = reader.readLine();
        }
        reader.close();
        return sb.toString();
    }
    

    use it as follows:

    readFromAssets(context,"test.txt")
    
    0 讨论(0)
  • 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<String> 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<String>();
            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();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 14:50

    Here is a simple class that handles both raw and asset files :

    public class ReadFromFile {

    public static String raw(Context context, @RawRes int id) {
        InputStream is = context.getResources().openRawResource(id);
        int size = 0;
        try {
            size = is.available();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        return readFile(size, is);
    }
    
    public static String asset(Context context, String fileName) {
        InputStream is = null;
        int size = 0;
        try {
            is = context.getAssets().open(fileName);
            AssetFileDescriptor fd = null;
            fd = context.getAssets().openFd(fileName);
            size = (int) fd.getLength();
            fd.close();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        return readFile(size, is);
    }
    
    
    private static String readFile(int size, InputStream is) {
        try {
            byte buffer[] = new byte[size];
            is.read(buffer);
            is.close();
            return new String(buffer);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
    

    }

    For example :

    ReadFromFile.raw(context, R.raw.textfile);
    

    And for asset files :

    ReadFromFile.asset(context, "file.txt");
    
    0 讨论(0)
提交回复
热议问题