欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android采用File形式保存與讀取數(shù)據(jù)的方法

 更新時(shí)間:2016年06月23日 11:32:41   作者:hbiao68  
這篇文章主要介紹了Android采用File形式保存與讀取數(shù)據(jù)的方法,涉及Android文件流操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Android采用File形式保存與讀取數(shù)據(jù)的方法。分享給大家供大家參考,具體如下:

將數(shù)據(jù)直接以文件的形式保存在設(shè)備中,通過(guò)Context.openFileInput()方法獲得標(biāo)準(zhǔn)的JAVA文件輸入流(FileInputStream),通過(guò)Context.openFileOutput()方法獲得標(biāo)準(zhǔn)的JAVA文件輸出流(FileOutputStream)

寫(xiě)數(shù)據(jù)到file文件中

findViewById(R.id.file).setOnClickListener(new Button.OnClickListener() {
  @Override
  public void onClick(View v) {
    try {
    //可寫(xiě)入的方式創(chuàng)建或打開(kāi)huangbiao.txt文件
    //該文件的路徑是/data/data/包名/files/huangbiao.txt
      FileOutputStream fos = openFileOutput("huangbiao.txt",Context.MODE_APPEND);
    //將字符串寫(xiě)入到文件中
      fos.write("huangbiao".getBytes());
    //關(guān)閉數(shù)據(jù)流
      fos.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
});

讀取數(shù)據(jù)的方法

findViewById(R.id.read_file).setOnClickListener(new Button.OnClickListener() {
  @Override
  public void onClick(View v) {
    FileInputStream fis;
    try {
    //打開(kāi)文件并得到InputStream對(duì)象
      fis = openFileInput("huangbiao.txt");
    //available()返回估算需要的空間長(zhǎng)度
      byte[] buffer = new byte[fis.available()];
    //把數(shù)據(jù)流的內(nèi)容寫(xiě)入buffer中
      fis.read(buffer);
      String aaa = new String(buffer);
      System.out.println(aaa);
      fis.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
});

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android文件操作技巧匯總》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論