无法理解如何计算线条。这是我的代码。
void load() throws IOException
{
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"agenda.file");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
TextView te=(TextView)findViewById(R.id.textView1);
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
Button monpopb = (Button) findViewById(R.id.button13);
monpopb.setText(text);
} 那么,如何在TextView中计算和设置文本?谢谢!
答案 0 :(得分:2)
这是你要找的吗?
void load() throws IOException
{
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"agenda.file");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
TextView te=(TextView)findViewById(R.id.textView1);
int lineCount = 0;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
lineCount++;
}
te.setText(String.valueOf(lineCount));
}
catch (IOException e) {
//You'll need to add proper error handling here
}
Button monpopb = (Button) findViewById(R.id.button13);
monpopb.setText(text);
}
答案 1 :(得分:0)
对于已经存在的文本文件,可以使用LineNumberReader
类以及LineNumberReader.skip()
和LineNumberReader.getLineNumber()
方法来获取文件中的总行数。像这样:
...
InputStream inputStream = new FileInputStream(new File("<FILE_NAME>"));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
LineNumberReader lineNumberReader = new LineNumberReader(bufferedReader);
try {
lineNumberReader.skip(Long.MAX_VALUE);
} catch (IOException e) {
e.printStackTrace();
}
linesInFile = lineNumberReader.getLineNumber() + 1; // because line numbers starts from 0
...