如何在 android 中逐行读取 .txt 文档?
How to read a .txt document line by line in android?
在此代码中,仅读取在 .txt 文件中输入的最后一个单词。当满足条件时,我调用相同的 activity。我应该对代码进行哪些更改,以便它读取一个单词,再次调用相同的 activity 然后读取一个新单词?
public class Page extends Activity {
Button b;
TextView t;
EditText e;
int i = 0;
String str2;
String w, x=null;
static BufferedReader c = null;
static BufferedReader ic = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
b = (Button) findViewById(R.id.benter);
t = (TextView) findViewById(R.id.tv);
e = (EditText) findViewById(R.id.et);
InputStream f = getResources().openRawResource(R.raw.incorrect);
ic = new BufferedReader(new InputStreamReader(f));
try {
while ((w = ic.readLine()) != null) {
t.setText(w);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str2 = e.getText().toString();
if (str2.equals(x)) {
Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
Intent open = new Intent(Page.this, Page.class);
startActivity(open);
} else {
Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
e.setText("");
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
您可以将 Apache Commons I/O 库用于 this.It 如此简单。并保持您的代码美观。为什么要从资产中获取 .txt 文件?
AssetManager am = context.getAssets();
InputStream fs = am.open("a.txt");
List<String> lines = IOUtils.readLines(file,"UTF-8");
您可以使用此代码逐行读取文件
try {
FileReader fileReader = new FileReader("FilePath");
BufferedReader reader = new BufferedReader(fileReader);
String data = null;
StringBuilder builder = new StringBuilder();
while ((data = reader.readLine()) != null) {
builder.append(data);
}
System.out.println(builder.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在您的代码中,您的 onclick 侦听器有问题..请将 onclick 侦听器置于 while 循环之外..
您的 onClickListener() 中存在轻微错误:
public class Page extends Activity {
Button b;
TextView t;
EditText e;
int i = 0;
String str2;
String w, x=null;
static BufferedReader c = null;
static BufferedReader ic = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
b = (Button) findViewById(R.id.benter);
t = (TextView) findViewById(R.id.tv);
e = (EditText) findViewById(R.id.et);
InputStream f = getResources().openRawResource(R.raw.incorrect);
ic = new BufferedReader(new InputStreamReader(f));
try {
while ((w = ic.readLine()) != null) {
t.setText(w);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str2 = e.getText().toString();
// it was str2.equals(x) nut x is null, replace w with x
if (str2.equals(w)) {
Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
Intent open = new Intent(Page.this, Page.class);
startActivity(open);
} else {
Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
e.setText("");
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
此代码适合您
您可以将文本文件保存在项目的 "Assets" 文件夹中,并使用以下代码在 java class
中检索该文件
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("YOUR_TEXT_FILE.txt")));
StringBuilder total = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
total.append(line);
}
message=total.toString();
System.out.println(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
之后你在 String message 中有了那个文件
您的程序正在读取最后一行,因为您每次调用 readLine() 方法时都会覆盖 'w' 变量。你应该做的是将你的 'w' 变量设置为空,并且 append 它与 readLine() 保持文本中的所有行已读取的文件,然后调用 setText() 一次以将所有内容放入您的视图中。如果不附加,则每次 while 循环运行时,您基本上都会覆盖 'w' 变量,这就是为什么您从文本文件中收到最后一行的原因。
在此代码中,仅读取在 .txt 文件中输入的最后一个单词。当满足条件时,我调用相同的 activity。我应该对代码进行哪些更改,以便它读取一个单词,再次调用相同的 activity 然后读取一个新单词?
public class Page extends Activity {
Button b;
TextView t;
EditText e;
int i = 0;
String str2;
String w, x=null;
static BufferedReader c = null;
static BufferedReader ic = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
b = (Button) findViewById(R.id.benter);
t = (TextView) findViewById(R.id.tv);
e = (EditText) findViewById(R.id.et);
InputStream f = getResources().openRawResource(R.raw.incorrect);
ic = new BufferedReader(new InputStreamReader(f));
try {
while ((w = ic.readLine()) != null) {
t.setText(w);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str2 = e.getText().toString();
if (str2.equals(x)) {
Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
Intent open = new Intent(Page.this, Page.class);
startActivity(open);
} else {
Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
e.setText("");
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
您可以将 Apache Commons I/O 库用于 this.It 如此简单。并保持您的代码美观。为什么要从资产中获取 .txt 文件?
AssetManager am = context.getAssets();
InputStream fs = am.open("a.txt");
List<String> lines = IOUtils.readLines(file,"UTF-8");
您可以使用此代码逐行读取文件
try {
FileReader fileReader = new FileReader("FilePath");
BufferedReader reader = new BufferedReader(fileReader);
String data = null;
StringBuilder builder = new StringBuilder();
while ((data = reader.readLine()) != null) {
builder.append(data);
}
System.out.println(builder.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在您的代码中,您的 onclick 侦听器有问题..请将 onclick 侦听器置于 while 循环之外..
您的 onClickListener() 中存在轻微错误:
public class Page extends Activity {
Button b;
TextView t;
EditText e;
int i = 0;
String str2;
String w, x=null;
static BufferedReader c = null;
static BufferedReader ic = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
b = (Button) findViewById(R.id.benter);
t = (TextView) findViewById(R.id.tv);
e = (EditText) findViewById(R.id.et);
InputStream f = getResources().openRawResource(R.raw.incorrect);
ic = new BufferedReader(new InputStreamReader(f));
try {
while ((w = ic.readLine()) != null) {
t.setText(w);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str2 = e.getText().toString();
// it was str2.equals(x) nut x is null, replace w with x
if (str2.equals(w)) {
Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
Intent open = new Intent(Page.this, Page.class);
startActivity(open);
} else {
Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
e.setText("");
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
此代码适合您
您可以将文本文件保存在项目的 "Assets" 文件夹中,并使用以下代码在 java class
中检索该文件 try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("YOUR_TEXT_FILE.txt")));
StringBuilder total = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
total.append(line);
}
message=total.toString();
System.out.println(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
之后你在 String message 中有了那个文件
您的程序正在读取最后一行,因为您每次调用 readLine() 方法时都会覆盖 'w' 变量。你应该做的是将你的 'w' 变量设置为空,并且 append 它与 readLine() 保持文本中的所有行已读取的文件,然后调用 setText() 一次以将所有内容放入您的视图中。如果不附加,则每次 while 循环运行时,您基本上都会覆盖 'w' 变量,这就是为什么您从文本文件中收到最后一行的原因。