Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android 讀寫私有文件的函數

Android 讀寫私有文件的函數

編輯:高級開發

public String ReadSettings(Context context)

  {

  FileInputStream fIn = null;

  InputStreamReader isr = null;

  char[] inputBuffer = new char[255];

  String data = null;

  try{

  fIn = openFileInput("lee.txt");

  isr = new InputStreamReader(fIn);

  isr.read(inputBuffer);

  data = new String(inputBuffer);

  Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show();

  }

  catch (Exception e) {

  e.printStackTrace();

  Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();

  }

  finally {

  try {

  isr.close();

  fIn.close();

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  return data;

  }

  public void WriteSettings(String data)

  {

  FileOutputStream fOut = null;

  OutputStreamWriter osw = null;

  try{

  fOut = openFileOutput("lee.txt",MODE_PRIVATE);

  osw = new OutputStreamWriter(fOut);

  osw.write(data);

  osw.flush();

  //Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();

  }

  catch (Exception e) {

  e.printStackTrace();

  //Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();

  }

  finally {

  try {

  osw.close();

  fOut.close();

  } catch (IOException e) {

  e.printStackTrace();

  }

  }

  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved