I am trying to read from a text file, splitting the contents when a new line character is met and displaying the result in a TextView.
This is the readfromfile method that I have:
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;
}
And this is where the method is called:
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);
I think the issue might be the append
that I am using with the string to display, i.e. I am returning a string with appended text.
This is the View output:
I am trying to load from a file called history.txt, which contains all SMS's that have been sent using the app in string format.
This is the writeToFile method:
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());
}
}
Any ideas?
Perhaps you should use a ListView instead. Each row of the ListView can be a single TextView for a single line from the file or can be multiple TextViews if you want to break the data into nicely formatted columns.