如何在从文件中读取文本时在EditText字段中插入新行

时间:2015-09-12 14:14:39

标签: android android-edittext

我正在尝试创建一个Android应用程序,在其中它将获取资产中的文件内容,将内容复制到filestorage中的文件。然后在显示时,它将读取filestorage中的文件,并在edittext中追加这些行。 我面临的问题是没有读取空行。

以下是我正在阅读资产文件内容并将其复制到文件“current.txt”中的代码片段,该文件将存储在filestorage中。

bliss -directed -can -sh=f test.dimacs
Generator: (2,4)(10,18)(11,20)(12,19)(13,21)(40,76)(41,77)(42,78)(43,79)(44,80)(45,81)(46,82)(47,83)(48,84)(49,85)(50,86)(51,87)(52,88)(53,89)(54,90)(55,91)(56,92)(57,93)
Generator: (1,2)(3,4)(6,10)(7,11)(8,13)(9,12)(14,18)(15,20)(16,21)(17,19)(22,43)(23,42)(24,41)(25,40)(26,57)(27,56)(28,55)(29,54)(30,53)(31,52)(32,51)(33,50)(34,49)(35,48)(36,47)(37,46)(38,45)(39,44)(58,79)(59,78)(60,77)(61,76)(62,93)(63,92)(64,91)(65,90)(66,89)(67,88)(68,87)(69,86)(70,85)(71,84)(72,83)(73,82)(74,81)(75,80)(94,95)
Canonical labeling: (1,4)(6,20,14,21)(7,16,8,9,12,11,15,17,13)(10,19)(22,77,66,24,68,30,25,73,88,90,39,44,43,75,45,29,41,67,92,54,40,71,51,32,33,49,83,34,85,82,86,46,87,31,93,64,80,42,61,72,37,89)(23,58,76,70,84,52,91,78,60,69,48,53)(26,62,63,57,65,38)(27,56,55,79,74)(28,81)(35,50,47)
Nodes:          6
Leaf nodes:     4
Bad nodes:      0
Canrep updates: 1
Generators:     2
Max level:      2
|Aut|:          8
Total time:     0.06 seconds

bliss -directed -can -sh=fsm test.dimacs
Generator: (2,4)(10,18)(11,20)(12,19)(13,21)(40,76)(41,77)(42,78)(43,79)(44,80)(45,81)(46,82)(47,83)(48,84)(49,85)(50,86)(51,87)(52,88)(53,89)(54,90)(55,91)(56,92)(57,93)
Generator: (1,3)(6,14)(7,15)(8,16)(9,17)(22,58)(23,59)(24,60)(25,61)(26,62)(27,63)(28,64)(29,65)(30,66)(31,67)(32,68)(33,69)(34,70)(35,71)(36,72)(37,73)(38,74)(39,75)
Generator: (1,2)(3,4)(6,10)(7,11)(8,13)(9,12)(14,18)(15,20)(16,21)(17,19)(22,43)(23,42)(24,41)(25,40)(26,57)(27,56)(28,55)(29,54)(30,53)(31,52)(32,51)(33,50)(34,49)(35,48)(36,47)(37,46)(38,45)(39,44)(58,79)(59,78)(60,77)(61,76)(62,93)(63,92)(64,91)(65,90)(66,89)(67,88)(68,87)(69,86)(70,85)(71,84)(72,83)(73,82)(74,81)(75,80)(94,95)
Canonical labeling: (1,2,4,3)(6,21,8,7,15,14,20,16)(9,13)(10,19)(11,17,12)(22,75,42,59,60,66)(23,61,72,34,83,36,35,53,25,73,86,48,51,31,93,62,64,80,44,45,27,55,79,76,70,82,88,90,38,29,41,69,46,89,24,67,92,56,57,63,54,39,43,77,68,32,33,47,37,87,30)(26,65,40,71,52,91,78,58,74,28,81)(49,85,84,50)(94,95)
Nodes:          9
Leaf nodes:     4
Bad nodes:      0
Canrep updates: 1
Generators:     3
Max level:      3
|Aut|:          8
Total time:     0.06 seconds

以下是我在其中添加“current.txt”文件内容的edittext的代码段

reader = new BufferedReader(new              InputStreamReader(getAssets().open("pravachan.txt")));


   ContextWrapper contextWrapper=new    ContextWrapper(getApplicationContext());
    File directory= contextWrapper.getDir("FileStorage",Context.MODE_PRIVATE);
                File myInternalFile=new File(directory,"current.txt");

                FileWriter filewriter = new FileWriter(myInternalFile);
                   BufferedWriter out = new BufferedWriter(filewriter);


                String mLine = reader.readLine();

                    while (mLine != null)
                    {
                        //process line
                        if(mLine.isEmpty())
                        {
                            out.write(" ");
                            String str = System.getProperty("line.separator");
                            out.write(str);
                        }

                        else
                            out.write(mLine);


                        mLine = reader.readLine();


                    }
                    out.close();

我不希望跳过空行。我希望文件在Edittext中显示。请帮忙。提前谢谢!

1 个答案:

答案 0 :(得分:0)

我得到了答案! 我没有做out.write(str),其中str是行分隔符,而是out.newline()。它奏效了!