テキストファイルから読み込みしようとしていますが、改行文字が満たされたときに内容を分割し、結果をTextViewに表示しようとしています。BufferedReaderを使用しているときに新しい行に分割する
これは私が持っている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
かもしれないと思います表示する文字列、つまり、テキストが追加された文字列を返します。
これが表示出力されます:
私は文字列形式でアプリを使用して送信されたすべてのSMSのを含んでHISTORY.TXT、という名前のファイルから読み込むしようとしています。
これは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());
}
}
任意のアイデア?
あなたはどんな問題がありますか?何かがおかしい? – greenapps
@greenappsはい、申し訳ありませんが、上の出力を画面上で編集しました。それは、分離されたエントリの代わりにテキストの塊を表示しています。それは望ましい出力になります。 – firepro20
もちろん、最初にどの出力をロードしようとしているのかを教えてください。どのように見えるべきかをどのように知っていますか? – greenapps