[英]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文件编写的数据。
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());
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.