簡體   English   中英

如何在Android中寫入和讀取文本文件?

[英]How to write to and read from a text file in 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確實存在(在Eclipse中的data / data / my_project / files中),但是它僅包含一個最近添加的單詞。 此外,如果長按“收藏夾”按鈕,則會強制關閉該應用程序。

我做錯了什么? 如果你們能幫助我解決這個問題,我非常感謝。 非常感謝你。

========

編輯

非常感謝您的幫助。 我更新了代碼,以反映您的評論和提示。 到目前為止,已有一些改進:最喜歡的單詞已像word2 word2 word3一樣被寫入文件myFavourite.txt中 (盡管我希望它們顯示在新行中)。

但是,長按“收藏夾”按鈕時,“收藏夾”列表仍未加載。

實際上,我的目的是使可以在應用程序中加載“收藏夾”列表,並允許用戶選擇要再次查找的單詞。

非常感謝您的幫助。

在這條線上

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

如果每次創建流時文件已經存在,則您正在覆蓋該文件。 您要做的是傳遞MODE_APPEND而不是0。看看文檔

關於長點擊,這些行

if (instream) {

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

甚至不應該編譯。 你想要的可能是這樣的

if (instream.ready()) {

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

看這個例子,它很容易在android中讀取文本文件。

文件必須保留。

res / raw / myFavourite.txt。

和你的問題一樣。 如何在Android中從SD卡讀取文本文件?

另一個簡單的教程。如何閱讀文本文件。 http://android-er.blogspot.com/2010/07/display-text-file-in-resraw_01.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM