简体   繁体   English

您如何在Android中读取.txt文件?

[英]How do you read a .txt file in Android?

I am newbie in android development. 我是android开发的新手。 Today for i was trying to display all my practiced programs of java in my application. 今天,因为我试图在我的应用程序中显示我所有实践过的Java程序。 I want the application to read the data written in .txt file. 我希望应用程序读取以.txt文件编写的数据。

  1. In which folder should I store all my programs? 我应该将所有程序存储在哪个文件夹中? They are more than 100. 他们超过100。
  2. I want to display the content of program 2 when I clicked the 2 on the list view or any other 当我在列表视图或任何其他视图上单击2时,我想显示程序2的内容
  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. 您可以将文本文件保留在raw / assets文件夹中。 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. 因为您是Java程序员,所以无需告诉如何从InputStream读取数据,如果您确实想要,请告诉我,我将发布其余代码。

Saving that huge amount of data in data base is not a good idea. 在数据库中保存大量数据不是一个好主意。

Example to read data from InputStream 从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. 是的,答案会很长,我认为有关SQLite的教程将是一个开始的地方。 2,1. 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); @Override public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState); setContentView(R.layout.activity_read); 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.
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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