Привет, я записываю имя и номера в файл в Android, а затем открываю файл журнала и показываю его в TextView. но я не могу добавить "новую строку" (" \n " ), поэтому он показывает:
Имя1 №1 Имя2 №2 Имя3 №3 Имя4 №4
я получаю "беспорядок" только в одной длинной строке...
я пробую следующее:
.....
addNameAndNrToLogg(stringName, stringNr); //call ..
.....
//save the logging
public void addNameAndNrToLogg(String Name, String Nr) {
String content = Name + " " + Nr + " " + "\\\n" + " ";
content = content.replace("\\\n", System.getProperty("line.separator"));
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriteropenFileOutput("logg.txt", Context.MODE_APPEND));
outputStreamWriter.write(content.toString());
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
......
loggfil = getLogg(); //open the loggfile and save this to a string (just an ordinary open file...)
TextView tv = new TextView(this);
tv.setText(loggfil); // set content to loggfile
tv.setMovementMethod(new ScrollingMovementMethod());//make it scrollable
setContentView(tv); //display it..
.....
// я также пытался добавить \n, \n, \\n, и ничего не устанавливало триггер "новой строки"..
//У тебя есть какие-либо идеи?? :-)
РЕДАКТИРОВАТЬ: я просто хочу быть более точным: у меня есть сейчас, и это не работает :( :
....
public void addMatchToLogg(String Name, String Nr) {
String content = Name + " " + Nr + " " + System.getProperty("line.separator");
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("logg.txt", Context.MODE_APPEND));
outputStreamWriter.write(content.toString());
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
....
// then in a other activity i open the logfile and put it in a textView:
loggfil = getLogg();
TextView tv = new TextView(this);
tv.setText(loggfil);
tv.setMovementMethod(new ScrollingMovementMethod());
setContentView(tv);
...
}
..
//then the getLogg():
public String getLogg() {
String loggBuffer = "emty logg";
try {
InputStream inputStream = openFileInput("logg.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 ) {
stringBuilder.append(receiveString);
}
inputStream.close();
loggBuffer = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
return loggBuffer;
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
return loggBuffer;
}
return loggBuffer;
}
результат: Имя1 №1 Имя2 №2 Имя3 №3 Имя4 №4
а не: Имя1 №1 Имя2 №2 Имя3 №3 Имя4 №4
в XML у меня есть:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingTop="20dp"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical" >
<TextView
android:id="@+id/logg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/emty"
android:textSize="21sp"
android:maxLines="5"
android:scrollbars="vertical"/>
</LinearLayout>
ПРИМЕЧАНИЕ: и когда я вытаскиваю файл logg.txt из «эмулятора», он «неправильный»/noNewLine: Name1 nr1 Name2 nr2 Name3 nr3 Name4 nr4
отредактировано только для теста и «комментария». Я проверил это: я проверил «принудительное значение строки» с помощью:
Name = "Event:";
Nr = "123";
а потом:
String content = siteName + "_" + shortDate + "_" ;
content = content + System.getProperty("line.separator");
//(yes I know i probably want to use stringbuilder, or is it a must here?..)
а затем отправить его в журнал, 3 раза, результат:
Событие:_123_Событие:_123_Событие:123
и не:
Событие:123
Событие:123
Событие:123
_________ :-(