Android文件保存和读取

来源:互联网 发布:网络炒作方案 编辑:程序博客网 时间:2024/04/30 03:59

 

  1. public class DataActivity extends Activity {      
  2.     private EditText filenameText;    
  3.     private EditText contentText;     
  4.     private TextView resultView;      
  5.     private static final String TAG = "DataActivity";     
  6.     /** Called when the activity is first created. */     
  7.     @Override         
  8.         public void onCreate(Bundle savedInstanceState) {         
  9.         super.onCreate(savedInstanceState);       
  10.         setContentView(R.layout.main);        
  11.         filenameText = (EditText) this.findViewById(R.id.filename);       
  12.         contentText = (EditText) this.findViewById(R.id.content);         
  13.         resultView = (TextView) this.findViewById(R.id.result);       
  14.         String filename = filenameText.getText().toString();          
  15.         Button button = (Button) this.findViewById(R.id.button);          
  16.         Button showButton = (Button) this.findViewById(R.id.showButton);          
  17.         button.setOnClickListener(listener);          
  18.         showButton.setOnClickListener(listener);          
  19.     }     
  20.     private View.OnClickListener listener = new View.OnClickListener() {                  
  21.         @Override             
  22.             public void onClick(View v) {             
  23.             Button button = (Button) v;           
  24.             String filename = filenameText.getText().toString();              
  25.             switch(button.getId()){               
  26.             case R.id.button://如果是保存按钮                
  27.                 int resId = R.string.success;                                                 
  28.                 String content = contentText.getText().toString();                
  29.                 try {                                                             
  30.                     OutputStream outStream = DataActivity.this.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);  
  31.                     //四中操作模式  
  32.                     //Context.MODE_PRIVATE=0 覆盖、私有  
  33.                     //Context.MODE_APPEND=32768追加、私有  
  34.                     //Context.MODE_WORLD_READABLE=1其他的程序可以访问  
  35.                     //Context.MODE_WORLD_WRITEABLE=2  
  36.                     try {                         
  37.                         FileService.save(outStream, content);//保存文件                       
  38.                     } catch (Exception e) {                       
  39.                         Log.e(TAG, e.toString());                         
  40.                         resId = R.string.error;                       
  41.                     }     
  42.                 } catch (FileNotFoundException e) {               
  43.                     Log.e(TAG, e.toString());                     
  44.                     resId = R.string.error;                   
  45.                 }                 
  46.                 Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG).show();               
  47.                 break;                
  48.             case R.id.showButton://如果是显示按钮                
  49.                 try {                     
  50.                     InputStream inStream = DataActivity.this.openFileInput(filename);                 
  51.                     String text = FileService.read(inStream);                     
  52.                     resultView.setText(text);                     
  53.                 } catch (Exception e) {                   
  54.                     Log.e(TAG, e.toString());  
  55.                     resId = R.string.error;  
  56.                     Toast.makeText(DataActivity.this"读取失败", Toast.LENGTH_LONG).show();      
  57.                 }  
  58.                 break;    
  59.             }     
  60.         }     
  61.     };    
  62. }  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/702531

0 0
原创粉丝点击