2017-01-22 16 views
0

テキストファイルから読み込みしようとしていますが、改行文字が満たされたときに内容を分割し、結果を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かもしれないと思います表示する文字列、つまり、テキストが追加された文字列を返します。

これが表示出力されます:

Output

私は文字列形式でアプリを使用して送信されたすべての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()); 
     } 
    } 

任意のアイデア?

+0

あなたはどんな問題がありますか?何かがおかしい? – greenapps

+0

@greenappsはい、申し訳ありませんが、上の出力を画面上で編集しました。それは、分離されたエントリの代わりにテキストの塊を表示しています。それは望ましい出力になります。 – firepro20

+0

もちろん、最初にどの出力をロードしようとしているのかを教えてください。どのように見えるべきかをどのように知っていますか? – greenapps

答えて

0

代わりにListViewを使用する必要があります。 ListViewの各行は、ファイルからの単一行の単一のTextViewにすることができます。また、データをきれいにフォーマットされた列に分割したい場合は、複数のTextViewにすることができます。

0

Javadocを確認してください。 readLine()は、ラインターミネータを削除します。出力を行うときに行が必要な場合は、行終端子を自分で追加する必要があります。この場合は、GUIにデータを表示するときに、読んだときと同じように\nまたは\r\nを追加する必要があります。

0

これは条件を追加することによって解決されました。変数の量が設定されており、変更されることはありませんので、すべての8本の読出し線に、2つの行はそうのようにスキップされます:

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は最初からやり直すべきだったはずです。

関連する問題