【发布时间】:2014-07-07 15:31:15
【问题描述】:
您好,我将姓名和数字记录到 android 中的文件中, 然后我打开日志文件,并在 TextView 中显示。 但我不能添加“换行符”(“\n”)所以它显示:
姓名1 nr1 名称2 nr2 Name3 nr3 名称4 nr4
我只在一长串中收到“一团糟”...
我尝试以下方法:
.....
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 nr1 姓名2 nr2 姓名3 nr3 姓名4 nr4
而不是: 姓名1 nr1 名称2 nr2 Name3 nr3 Name4 nr4
在我拥有的 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?..)
然后发送到logg,3次,结果:
事件:_123_事件:_123_事件:123
而不是:
事件:123
事件:123
事件:123
_________ :-(
【问题讨论】:
-
所以文件本身没有新行?
-
完全正确 :-( " Name1 nr1 Name2 nr2 Name3 nr3 Name4 nr4 "
-
您的文件是否有可能包含带有空格的元素,例如名称一 nr1 名称二 nr2 名称三 nr3 ?
-
WOW 有趣的是:testNAME 1 Nr 08 12:58:11 testNAME 2 Nr 08 12:59:16 .. 而不是:testNAME 1 Nr 08 12:58:11 testNAME 2 Nr 08 12 :59:16 .. 我测试了“强制字符串值”:Name =“Event:”;编号=“123”;然后:字符串内容 = siteName + "" + shortDate + "" ;内容 = 内容 + System.getProperty("line.separator");然后将其发送到日志,2次,结果:事件:_123_事件:_123_事件:_123_而不是:事件:_123_“+新行”事件:_123_“+新行”事件:_123_“+新行”
-
注意:是否可以写“logg”,以便“最新名称”在前而不是最后?:
标签: android textview newline logfile