android 開發中向文件指定位置寫入數據

File mFile = new File(Environment.getExternalStorageDirectory()+"/test.txt"); try { Log.v(tag,"====onCreate======"+Environment.getExternalStorageDirectory()+"/test.txt"); try { if(!mFile.exists()){ Log.v(tag,"====onCreate======="+mFile.exists()); Log.v(tag,"====onCreate===start===="+mFile.createNewFile()); }else{ Log.v(tag,"====onCreate===0===="); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } Log.v(tag,"====onCreate===1===="); RandomAccessFile mRandomAccessFile = new RandomAccessFile(mFile, "rw"); Log.v(tag,"====onCreate===2===="); try { mRandomAccessFile.writeBytes("1234567890"); mRandomAccessFile.seek(3); Log.v(tag,"====onCreate===3===="); mRandomAccessFile.writeBytes("aaaa"); Log.v(tag,"====onCreate===4===="); byte [] data = new byte[50]; Log.v(tag,"====onCreate===4===="+mRandomAccessFile.read(data)); Log.v(tag,"====onCreate===end==="+data); mRandomAccessFile.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }
相關文章
相關標籤/搜索