Re: [問題] 讀取文字檔(/raw;/assets;/res)

看板AndroidDev作者 (別理我)時間12年前 (2011/12/29 11:27), 編輯推噓1(106)
留言7則, 2人參與, 最新討論串5/6 (看更多)
請問大大, 我將檔案*.txt放置sd卡後~ 在debug模式下執行到 FileOutputStream outputStream = new FileOutputStream(file); 就會跳到catch中.. 請問是否還有其他細節須注意的呢?謝謝 ※ 引述《givemepass (〆)》之銘言: : 把你的檔案放在assets裡面 : 我假設你的檔案叫做 my_text_file.txt : 然後我複製一份存到sdcard下面/sdcard/text_file.txt : 就可以拿這個來讀取 : public class TestRandomFileAccessActivity extends Activity { : /** Called when the activity is first created. */ : @Override : public void onCreate(Bundle savedInstanceState) { : super.onCreate(savedInstanceState); : setContentView(R.layout.main); : //先取得資產管理員 : AssetManager assetManager = this.getApplicationContext().getAssets(); : try { : //用資產管理員打開文字檔 變成串流 : InputStream inputStream = assetManager.open("my_text_file.txt"); : byte[] b = new byte[1024]; : int len = -1; : File file = new File("/sdcard/text_file.txt"); : FileOutputStream outputStream = new FileOutputStream(file); -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.130.55.241

12/29 12:27, , 1F
他是把原本放在asset裡的文字檔讀出來寫到sdcard 不是直
12/29 12:27, 1F

12/29 12:27, , 2F
接放在sacard 如果直接放sdcard 那就直接File去接了
12/29 12:27, 2F

12/29 12:28, , 3F
至於為啥不讀出後就直接利用 還要多一個寫入的動作
12/29 12:28, 3F

12/29 12:29, , 4F
是因為原po說要用RandomAccessFile
12/29 12:29, 4F

12/29 12:30, , 5F
其實應該要先釐清 檔案到底要放在哪 內容是如何 多大
12/29 12:30, 5F

12/29 12:31, , 6F
然後再來看是不是一定要用RandomAccessFile
12/29 12:31, 6F

12/29 12:56, , 7F
推K大 他的問題是出在沒有在manifest開權限
12/29 12:56, 7F
文章代碼(AID): #1E-zuuly (AndroidDev)
討論串 (同標題文章)
文章代碼(AID): #1E-zuuly (AndroidDev)