簡單的Android對檔案進行讀寫操作
來源:互聯網
上載者:User
環境:1.系統:window72.Android版本:2.23.eclipse3.5.2(伽利略)一、基本的流程圖(寫入檔案) 二、模擬器運行介面 1.主介面 2.儲存(不符合條件) 3.正常儲存 4.讀取檔案 5.關於編者 三、代碼如下: 1.儲存檔案的主要代碼 儲存的主代碼 1 bSave.setOnClickListener(new View.OnClickListener() { 2 3 @Override 4 public void onClick(View v) { 5 // TODO Auto-generated method stub 6 resId=R.string.success; 7 file_name=filename.getText().toString(); 8 file_content=filecontent.getText().toString(); 9 /* openFileOutput API10 * FileOutputStream openFileOutput (String name, int mode)11 * Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.12 */13 /* Context四種模式的API14 * Context.MODE_APPEND -->if the file already exists then write data to the end of the existing file instead of erasing it.15 * Context.MODE_PRIVATE -->the default mode, where the created file can only be accessed by the calling application (or all applications sharing the same user ID). 16 * Context.MODE_WORLD_READABLE -->allow all other applications to have read access to the created file.17 * Context.MODE_WORLD_WRITEABLE -->allow all other applications to have write access to the created file.18 */19 flag=InsertInfo();20 if(flag){21 try{22 OutputStream outStream=FileStoreActivity.this.openFileOutput(file_name, MODE_PRIVATE);23 FileService.save(outStream, file_content);24 25 }catch(Exception e){26 Log.e(TAG, e.toString());27 resId=R.string.error;28 }29 Toast.makeText(FileStoreActivity.this, resId, Toast.LENGTH_LONG).show();30 }31 }32 33 34 }); 2.讀取檔案的主要代碼 讀取主代碼 1 Button bRead=(Button)findViewById(R.id.read); 2 bRead.setOnClickListener(new View.OnClickListener() { 3 4 @Override 5 public void onClick(View v) { 6 // TODO Auto-generated method stub 7 //此時隱藏了檔案內容框和檔案內容的label 8 filecontent.setVisibility(View.GONE); 9 TextView content=(TextView)findViewById(R.id.content);10 content.setVisibility(View.GONE);11 InputStream inStream;12 file_name=filename.getText().toString();13 14 /* API15 * FileInputStream openFileInput (String name) 16 * Open a private file associated with this Context's application package for reading. 17 */18 try {19 inStream = FileStoreActivity.this.openFileInput(file_name);20 String context=FileService.read(inStream).toString();21 Toast.makeText(FileStoreActivity.this, context, Toast.LENGTH_LONG).show();22 } catch (Exception e) {23 Log.e(TAG,e.toString());24 resId=R.string.error;25 }26 }27 }); 3.關於編者的代碼 關於編碼代碼Button bAbout=(Button)findViewById(R.id.about); bAbout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new AlertDialog.Builder(FileStoreActivity.this).setTitle("編者資訊") .setMessage( "編者: naive_monk" + "\n" + "愛好:籃球、音樂、交友" + "\n" + "Q Q:1271522052" + "\n" + "郵箱:summerdir@gmail.com" + "\n" + "所在城市:廣東惠州 " + "\n") .setCancelable(false).setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int id) { // TODO Auto-generated method stub dialog.dismiss(); } }).setNegativeButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int id) { // TODO Auto-generated method stub dialog.dismiss(); } }).show(); } }); 4.業務代碼 代碼/* * 儲存檔案 */ public static void save(OutputStream outStream, String content) throws Exception { // 寫入資料 outStream.write(content.getBytes()); outStream.close(); } /* * 讀取檔案 */ public static String read(InputStream inStream) throws Exception { // 位元組流 ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = -1; //擷取位元組資料 while ((len = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } //得到位元組資料 byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return new String(data); } 四、小結 這個例子只是簡單的操作檔案的讀寫,很多地方都不夠完善,希望有興趣的網友們可以互相交流下~~~