Re: [問題] 創造txt的問題

看板AndroidDev作者 (天)時間13年前 (2011/01/20 03:12), 編輯推噓6(601)
留言7則, 6人參與, 最新討論串2/2 (看更多)
感謝各為板友的幫忙 問題已解 來回一下自己的文給後人爬 ============================================================= Android Data Storage 的方法有五種 http://developer.android.com/guide/topics/data/data-storage.html 若你英文不好 這裡有中文版 http://charles-song.blogspot.com/2010/11/data-storage-in-android.html 我是使用 Internal Storage =========================創立檔案並寫入=============================== String FILENAME = "Text"; //看你創立的file要叫什麼名稱 String string = "hello world!"; //你想寫入的字串 deleteFile("hello_file"); //如果你想刪除某個file 就用delete //使用 openFileOutput 取得 FileOutputStream FileOutputStream fos = null; try { fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); } catch (FileNotFoundException e) { e.printStackTrace(); } try { fos.write(string.getBytes()); //寫入string } catch (IOException e){ e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } ================================讀取檔案內容============================ FileInputStream fis; try { fis = openFileInput(FILENAME); byte[] input = new byte[fis.available()]; while (fis.read(input) != -1) { textview.setText(new String(input)); //我是將取出來的字串用textview顯示 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } ========================================================================= 以上 如有錯誤請指證 謝謝收看 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.227.122.153

01/20 05:48, , 1F
解決後分享 推一個
01/20 05:48, 1F

01/20 07:49, , 2F
推解決辦法
01/20 07:49, 2F

01/20 07:57, , 3F
解決後分享. 推!!! (如果讀取+更新用 AsyncTask 會更好)
01/20 07:57, 3F
樓上你沒推阿~~XD 話說 你是在給我新作業嗎? 又是一個沒聽過的東西哪QAQ

01/20 15:18, , 4F
呵,俺又不是你老師。只是個良心的建議orz
01/20 15:18, 4F
那你給點方向 我來去研究看看好了XD ※ 編輯: nonebelieve 來自: 61.227.122.153 (01/20 15:37)

01/20 18:23, , 5F
有學到有推~~
01/20 18:23, 5F

01/20 21:28, , 6F
01/20 21:28, 6F

04/10 20:51, , 7F
太棒了!! 我正需要這個!! 大推大推!!!^^
04/10 20:51, 7F
文章代碼(AID): #1DDpUnHw (AndroidDev)
文章代碼(AID): #1DDpUnHw (AndroidDev)