简体   繁体   中英

How do you read a .txt file in Android?

I am newbie in android development. Today for i was trying to display all my practiced programs of java in my application. I want the application to read the data written in .txt file.

  1. In which folder should I store all my programs? They are more than 100.
  2. I want to display the content of program 2 when I clicked the 2 on the list view or any other
  3. Can we store the text files in database? If so how can I access them ? How can I read them?
  4. Any basic ideas how can I solve this?

You can kept text file in raw / assets folder. To read them just use this code. From Assets:

BufferedReader reader = new BufferedReader(
                 new InputStreamReader(getAssets().open("YourTextFile.txt")));

From Raw:

InputStream inputStream =  context.getResources().openRawResource(R.id.yourresoureid);

             InputStreamReader inputreader = new InputStreamReader(inputStream)

as you are a java programmer no need to tell how to read data from InputStream, if your really want then tell me I will post the rest of the code.

Saving that huge amount of data in data base is not a good idea.

Example to read data from InputStream

 BufferedInputStream bis=new BufferedInputStream(inputstream);
            ByteArrayBuffer baf=new ByteArrayBuffer(1000);
            while((k=bis.read())!=-1)
            {
            baf.append((byte)k);

            }
            String results=new String(baf.toByteArray());
  1. Start with something easy and work up to the database option.
  2. Yes, the answer would be quite long, and I think a tutorial on SQLite would be a place to start on this. 2,1. Try putting your text files in the assets folder and reading them like this. This code reads a file, and dumps it line by line into the log.

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read);

     AssetManager assetManager = getAssets(); try { BufferedReader br = new BufferedReader(new InputStreamReader( assetManager.open("hi.txt"))); // InputStream inputStream = assetManager.open("hi.txt"); // BufferedReader br = new BufferedReader( // new InputStreamReader(inputStream)); String lineIn; while ((lineIn = br.readLine()) != null) { Log.d("ReadTheDamnFile", lineIn); } assetManager.close(); } catch (IOException e) { } 

    }

try this its work fine :)

try 
   {
           if(poslist==0)
           {
               in = this.getAssets().open("file1.txt");
               iv.setBackgroundResource(R.drawable.fileimage1);

           }
 }
  catch (IOException e) 
  {
      e.printStackTrace();
  }
      try {
        reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
      String line="";
      String s ="";
   try 
   {
       line = reader.readLine();
   } 
   catch (IOException e) 
   {
       e.printStackTrace();
   }
      while (line != null) 
      {
       s = s + line;
       s =s+"\n";
       try 
       {
           line = reader.readLine();
       } 
       catch (IOException e) 
       {
           e.printStackTrace();
       }
    }
    tv.setText(""+s);
  }

  public void onClick(View v){
      try {
    line = reader.readLine();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
      if (line != null){
          tv.setText(line);
      } else {
          //you may want to close the file now since there's nothing more to be done here.
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM