I’m creating an app that requires a user to enter in some info about themselves. The info has to be a few paragraphs long, and to do this, they have to use the enter button on their phone’s keyboard to create new lines. This works but when the user comes back to edit their info, it has forgotten all the new lines that were made. For example
if they wrote
> line one
>
>
>
> line five
then when they open the app again it shows
line oneline five
The text from line five has been directly attached to the text in line one.
What do I do to make it remember new lines?
Here is my code for writing from the text box to a file, then reading from a file to the text box again:
public void save() {
String FILENAME = "item_1_content.txt";
String string = name.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.write(string.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String readSavedData ( ) {
String datax = "" ;
try {
FileInputStream fIn = openFileInput ( "item_1_content.txt" ) ;
InputStreamReader isr = new InputStreamReader ( fIn ) ;
BufferedReader buffreader = new BufferedReader ( isr ) ;
String readString = buffreader.readLine ( ) ;
while ( readString != null ) {
datax = datax + readString ;
readString = buffreader.readLine ( ) ;
}
isr.close ( ) ;
} catch ( IOException ioe ) {
ioe.printStackTrace ( ) ;
}
name.setText(String.valueOf(datax));
return datax ;
}
Use this:
You need to append the line break. It is encodet by
"\n".