我正在尝试从文本文件中读取,在满足新行字符时拆分内容并在TextView中显示结果。
这是我的readfromfile方法:
public String readFromFile(Context context) {
String ret = "";
try {
InputStream inputStream = context.openFileInput("history.txt");
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
//receiveString.split("\n");
//String[] split = receiveString.split("\n\\s*");
stringBuilder.append(receiveString);
// if((receiveString = bufferedReader.readLine()) == "\n"){
// }
}
inputStream.close();
//stringBuilder.
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
Toast.makeText(this, "You have not sent any messages!!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
Toast.makeText(this, "Cannot read from file!!", Toast.LENGTH_LONG).show();
}
return ret;
}
这就是调用方法的地方:
String message = readFromFile(getApplicationContext()); // is this really needed? yes it is, use with readFromFile()
TextView textView = new TextView(this);
params2.addRule(RelativeLayout.BELOW, history.getId()); // this is very important for spacing using ids
textView.setTextSize(14); //this might give an error or look weird on devices, horizontally and vertically
textView.setText(message); // simplest fix is to force device to stick with horizontal, no common device is bigger than 5.5"
//textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
textView.setId(View.generateViewId()); // This requires a minimum API 17
textView.setGravity(Gravity.LEFT | Gravity.BOTTOM);
layout.addView(textView, params2);
我认为问题可能是我使用的append
字符串要显示,即我返回带有附加文本的字符串。
这是视图输出:
我正在尝试从名为 history.txt 的文件加载,该文件包含使用字符串格式的应用程序发送的所有SMS。
这是writeToFile方法:
public void writeToFile(String data,Context context) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("history.txt", Context.MODE_APPEND));
//format data string here to include time stamp
BufferedWriter writer = new BufferedWriter(outputStreamWriter);
String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
outputStreamWriter.append("\n");
outputStreamWriter.append(data);
outputStreamWriter.append(" ");
outputStreamWriter.append(currentDateTime);
writer.newLine();
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
有什么想法吗?
答案 0 :(得分:0)
检查Javadoc。 readLine()
删除行终止符。如果您在输出时需要行,则必须自己添加行终止符:在这种情况下,您可以在阅读时添加\n
或\r\n
,因为您正在显示GUI中的数据。
答案 1 :(得分:0)
也许您应该使用ListView。 ListView的每一行可以是文件中单行的单个TextView,也可以是多个TextView,如果您想将数据分解为格式良好的列。
答案 2 :(得分:0)
这已通过添加和条件解决。由于变量的数量已设置且不会更改,因此在每8条读取行中,将跳过两行,如下所示:
while ( (receiveString = bufferedReader.readLine()) != null ) {
//receiveString.split("\n");
//String[] split = receiveString.split("\n\\s*");
stringBuilder.append(receiveString);
if(i == 8) {
stringBuilder.append("\n");
i = 0;
}
stringBuilder.append("\n");
// if((receiveString = bufferedReader.readLine()) == "\n"){
i++;
// }
}
谢谢你们的投入!
p.s正如@ Code-Apprentice指出的那样,ListView应该是从一开始就采用的方式。