开发者

Android filereading problem

i have problems with filewriting-reading. I am gonna be short on words. When user pushes OK btn a string is added to assignArr ArrayList. At this time, this string is saved to a file. When user adds a new string (by pushing OK btn) to Arraylist, only the former and the new item is in it, so there is no duplication. This is how i save the arraylist in the okbtn listener to a file:

    if (assignArr.size() > 0)
                  {
                    String filename = "tomato35.txt"; 
                     FileOutputStream fos; 
                    try { 
                        fos = openFileOutput(filename,Context.MODE_PRIVATE); 
                        ObjectOutputStream out = new ObjectOutputStream(fos); 
                        fos.flush();
                        
                        for (int t=0; t<assignArr.size(); t++)
                        {
                            out.writeUTF(assignArr.get(t).toString() + "\n");
                            Toast.makeText(MainActivity.this, "Saving OK + " + assignArr.get(t), Toast.LENGTH_LONG).show();
                        }
                                            
                        out.close(); 
                        } catch (FileNotFoundException e) { 
                           // TODO Auto-generated catch block 
                           e.printStackTrace(); 
                        } catch (IOException e) { 
                           // TODO Auto-generated catch block 
                           e.printStackTrace(); 
                        }
                      Toast.makeText(MainActivity.this, "assignArrsize: " + assignArr.size(), Toast.LENGTH_LONG).show();
 开发者_如何学JAVA                 } 

Lets suppose i added the String "Daddy Alex". So the output of this is "Saving OK + Daddy Alex", which is an item in the assignArr. Now i quit the application, so the assignArr Arraylist gets empty, as it is only a variable. Now i reopen my app and now the content of the file should be read into an arraylist:

try { 
               InputStream is = openFileInput("tomato35.txt");               
               BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
               String line; 
               String NL = System.getProperty("line.separator");                  
               while((line = br.readLine()) != null) 
               {
                      assignArr.add(line); 
               } 
               is.close(); 
             
               } 
               catch (IOException e) 
               { 
                  e.printStackTrace(); 
               } 
                for (int i=0; i<assignArr.size(); i++)
                {
                     Toast.makeText(MainActivity.this, "AssignArr." + i + " = " + assignArr.get(i), Toast.LENGTH_LONG).show();
                }

I am posting some outputs based on different strings in the assignArr:

Android filereading problem

I have also tried this:

String filename = "tomato35.txt"; 
BufferedWriter bw = null;
bw = new BufferedWriter(new FileWriter(filename, true));
for (int t=0; t<assignArr.size(); t++)
{
bw.write(assignArr.get(t).toString() + "\n");
Toast.makeText(MainActivity.this, "Saving OK + " + assignArr.get(t), Toast.LENGTH_LONG).show();
}
bw.flush();

and

PrintWriter f;
File file ;
file = new File("tomato44.txt");
f = new PrintWriter(file);
for (int t=0; t<assignArr.size(); t++)
{
f.println(assignArr.get(t).toString());
 Toast.makeText(MainActivity.this, "Saving OK + " + assignArr.get(t), Toast.LENGTH_LONG).show();
}
f.close();

But in these cases i don't even get the Toast msg...

i have no idea why are these wrong. Sometimes the output is ok at first time, but when i add another item to the assignArr, write to file, exit and then reopen the app, this second item is wrong, but the first is ok. I've been struggling with this for days now...


Make sure you have this in your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜