2012年7月17日 星期二

read/write text file from/into app private storage


public void writeFileData(String filename, String message){
if(GlobalConfig.DEBUG){
Log.w(TAG,"Write file:"+message);
}
        try{
            FileOutputStream fout= openFileOutput(filename, MODE_PRIVATE);
            byte[] b= message.getBytes("UTF-8");
            fout.write(b);
            fout.close();
        }catch(Exception e){
            e.printStackTrace();
        }

    }
    public void readFileData(String filename){
   
        String result="";
     
        try{
            FileInputStream fin= openFileInput(filename);
            int length= fin.available();
            byte[] b= new byte[length];
            fin.read(b);
            result= EncodingUtils.getString(b, "UTF-8");
            if(GlobalConfig.DEBUG){
    Log.w(TAG,"read file:"+result);
    }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

沒有留言:

張貼留言