標籤:操作 讀寫 tput 檔案的 ble 資料 try catch 模式
Android的I/O方法
方法 |
描述 |
public FileInputStream openFileInput(String name) |
設定要開啟的檔案輸入資料流 |
public FileOutputStream openFileOutput( String name,int mode) |
設定要開啟的檔案輸出資料流,指定操作模式,可以是0、MODE_APPEND、MODE_PRIVATE、MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE |
public Resources getResources() |
返回Resources對象 |
檔案的讀寫權限
openFileOutPut(String fliename,int model)
儲存資料至指定檔案名稱,第二個參數是檔案的操作模式,有以下四個常量值:
- MODE_PRIVATE:設定檔案屬性為私人,只能被當前項目讀寫
- MODE_APPEND:以添加方式開啟檔案,並向檔案中添加資料
- MODE_WORLD_READABLE:該檔案可被其他項目讀取
- MODE_WORLD_WRITEABLE: 該檔案可被其他項目寫入
儲存(寫入)檔案的操作
//讀取檔案的操作FileOutputStream fos=null; try { fos=openFileOutput("test.txt", Activity.MODE_PRIVATE); } catch (FileNotFoundException e) { e.printStackTrace(); }PrintWriter out=new PrintWriter(fos);out.print("白日依山盡");out.print("黃河入海流");out.close();儲存檔案的操作
讀取檔案的操作
//讀取檔案的操作try { FileInputStream fis=openFileInput("test.txt"); BufferedReader br=new BufferedReader(new InputStreamReader(fis)); String str=null; while((str=br.readLine())!=null){ TextView tv=(TextView) findViewById(R.id.TV); tv.setText(str); }} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();}讀取檔案的操作
檔案的讀寫【安卓8】