【问题标题】:Android - make a loggfile and display as a TextView in WITH new lines for every "events"?Android - 创建一个日志文件并在每个“事件”的新行中显示为 TextView?
【发布时间】: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


【解决方案1】:

试试这个

String content = Name + " " + Nr + " " + System.getProperty("line.separator") + " ";

【讨论】:

    【解决方案2】:

    经过大量测试后,我发现自己 像我以前那样在文件中添加新行是错误

    String content = Name + " " + Nr + " " + System.getProperty("line.separator");
    

    (然后将其保存到文件中,然后读取它..)

    字符串和文件可能有“2 个不同的 & unice line.separator”:-)

    所以我的解决方案:

    String content = siteName + " " + shortDate + "//n" ; 
    //the "//n" is just a "MARKER" for later..
    content = content.replaceAll("  ", " "); //just for nice things up..
    //and save to file...
    .....
    
    //then when the textview want to display this:
    // after the file have revived using getLogg()
            loggfil = getLogg();
    //then i look after the MARKER: "//n" and replace this..
            loggfil = loggfil.replaceAll("//n", System.getProperty("line.separator"));
    
              TextView tv = new TextView(this);
              tv.setText(loggfil);
              tv.setMovementMethod(new ScrollingMovementMethod());
              setContentView(tv);  
    

    结果是: 在文件中: Name1 Nr1//nName2 Nr2//nName3 Nr3//nName4 Nr4//n

    在“保存的字符串loggfil”中 名称1 Nr1 名称2 Nr2 名称3 Nr3 名称4 Nr4

    我很高兴,终于!!! :-D

    但是,仍然: 是否可以将“logg”写在最前面而不是最后一个? 我用:

                try {
                        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("logg.txt", 
                                Context.MODE_APPEND));
                        outputStreamWriter.write(content.toString());
                        //outputStreamWriter.write("test", 0, 4);
                        outputStreamWriter.close();
                    }
                    catch (IOException e) {
                        Log.e("Exception", "File write failed: " + e.toString());              
                    }
    

    【讨论】:

      猜你喜欢
      • 2011-02-23
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2014-02-03
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 2011-06-07
      相关资源
      最近更新 更多