Chinaunix首页 | 论坛 | 博客
  • 博客访问: 176722
  • 博文数量: 43
  • 博客积分: 1428
  • 博客等级: 上尉
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 09:33
文章分类

全部博文(43)

文章存档

2014年(3)

2013年(3)

2011年(1)

2010年(36)

分类: Java

2010-08-23 13:10:12

读:
public String ReadSettings(Context context){
      FileInputStream fIn = null;
      InputStreamReader isr = null;
      
      char[] inputBuffer = new char[255];
      String data = null;
      
      try{
       fIn = openFileInput("settings.dat");      
          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(Context context, String data){
      FileOutputStream fOut = null;
      OutputStreamWriter osw = null;
      
      try{
       fOut = openFileOutput("settings.dat",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();
                    }
          }
     }

使用方法:
WriteSettings(this,"setting0, setting1, setting2");
String data[] = ReadSettings(this).split(",");
阅读(875) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-08-25 08:30:07

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com