Java 使用BufferedReader Android时在新行拆分
我试图从文本文件中读取,在遇到新行字符时分割内容,并在TextView中显示结果 这是我拥有的readfromfile方法:Java 使用BufferedReader Android时在新行拆分,java,android,bufferedreader,Java,Android,Bufferedreader,我试图从文本文件中读取,在遇到新行字符时分割内容,并在TextView中显示结果 这是我拥有的readfromfile方法: public String readFromFile(Context context) { String ret = ""; try { InputStream inputStream = context.openFileInput("history.txt"); if ( inputStre
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());
}
}
有什么想法吗?查看Javadoc
readLine()
删除行终止符。如果在执行输出时需要行,则必须自己添加行终止符:在这种情况下,当您在GUI中显示数据时,在阅读时向每个类似项添加\n
或\r\n
。也许您应该改用ListView。ListView的每一行可以是文件中一行的单个文本视图,也可以是多个文本视图,如果您想将数据拆分为格式良好的列。这已通过添加和if条件解决。由于变量量已设置且不会更改,因此在每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、 正如@Code学徒指出的,ListView应该是从一开始就要走的路。你有什么问题?有什么问题吗?@greenapps是的,对不起,用屏幕上的输出编辑了上面的内容。它显示的是一个文本块,而不是一个单独的条目,这将是所需的输出。当然,您应该首先告诉我们尝试加载哪个texr,因为该输出没有告诉我们任何信息。我们怎么知道它应该是什么样子?@greenapps我正在尝试加载italic history.txt_italic_文件中包含的所有文本。我还在上面的帖子中添加了writeToFile代码。所以我想要的是在新行上加载和拆分,每个短信,每个条目都从“Customer”开始。你不应该显示编写文本文件的代码,而应该显示文本文件本身。