2011-11-06 50 views
1

我正在开发一个词典应用程序。有上,允许用户应用程序收藏夹按钮:如何在Android中写入和读取文本文件?

  • 短点击当前观看的单词添加到收藏夹列表;
  • 长按可查看收藏夹列表(添加的单词)。

到目前为止,我已经编码如下:

更新的代码:

//Writing lines to myFavourite.txt 
    btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite); 
    btnAddFavourite.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // Writing the content 
       try { 
       // opening myFavourite.txt for writing 
        OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myFavourite.txt", MODE_APPEND)); 
       // writing the ID of the added word to the file 
        out.write(mCurrentWord); 
       // closing the file 
        out.close(); 
       } catch (java.io.IOException e) { 
        //doing something if an IOException occurs. 
       } 
       Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT); 
       toast.show(); 
      } 
     }); 

    //Reading lines from myFavourite.txt 
    btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() {   

      @Override 
      public boolean onLongClick(View v) { 

      //trying opening the myFavourite.txt 
       try { 
      // opening the file for reading 
       InputStream instream = openFileInput("myFavourite.txt"); 

      // if file the available for reading 
       if (instream != null) { 
      // prepare the file for reading 
       InputStreamReader inputreader = new InputStreamReader(instream); 
       BufferedReader buffreader = new BufferedReader(inputreader); 

      String line; 

      // reading every line of the file into the line-variable, on line at the time 
      try { 
       while ((line = buffreader.readLine()) != null) { 
        // do something with the settings from the file 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      } 

      // closing the file again 
      try { 
      instream.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } catch (java.io.FileNotFoundException e) { 

      // ding something if the myFavourite.txt does not exits 
     } 
     return false; 

     }}); 
     } 

然而,收藏夹按钮并不与上面的代码行工作。

文件myFavourite.txt不会退出(数据/数据/ MY_PROJECT /文件在Eclipse中),但它仅包含一个最近增加的单词。此外,当长按“收藏夹”按钮时,应用程序会强制关闭。

有什么我已经做错了吗?如果你们能帮我解决这个问题,我非常感激。非常感谢你。

========

编辑

非常感谢您的帮助。我更新了我的代码以反映您的意见和提示。截至目前,已经有一些改进:最喜欢的词已被写入到文件myFavourite.txt单词2单词2 WORD3 ...(不过我希望他们能够出现在新的生产线)。

然而,当收藏夹按钮是长期的点击仍然没有加载收藏夹列表。

事实上,我的目的是使人们有可能加载应用程序内的收藏夹列表,并允许用户选择字(S)再次抬头。

非常感谢您的帮助。

回答

0

您不包括openFileOutput()的定义,但很可能您没有创建附加流(将内容添加到文件末尾),而是每次调用openFileOutput()时都重新创建文件。

我的水晶球,无法提供更多的信息。

编辑: 作为openFileOutput似乎是一个Android方法:所述第二参数指示所述写模式。至于要追加,你需要通过Context.MODE_APPEND而不是0: Context.MODE_APPEND

+0

'openFileOut'方法在上下文中定义,并且大概这个代码存在于一个活动中。 –

+0

啊,不知道Android本身,但这确实回答了我的水晶球问题:) –

+0

@Mark Rotteveel:非常感谢您的帮助。请查看我的更新代码,这些代码可以反映您的意见。 –

1

在此行中

OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myFavourite.txt",0)); 

要覆盖文件,如果它已经存在,每次创建流时间。你想要做的是传递MODE_APPEND而不是0.看看the documentation

关于长点击,这些行

if (instream) { 

while ((line = buffreader.readLine())) { 

甚至不应该编译。你想要什么可能是像

if (instream.ready()) { 

while ((line = buffreader.readLine()) != null) { 
    // Use this line 
} 
+0

非常感谢您的帮助。请查看我的更新代码,这些代码可以反映您的意见。 –