1 public class SD_Files_RW extends Activity implements OnClickListener{ 2 3 private String Text_of_input; 4 5 private Button button_01,button_02; 6 private EditText ed_text; 7 private TextView text_view; 8 9 private File sdCardDir; 10 private File saveFile; 11 private FileOutputStream outStream; 12 private FileInputStream inStream; 13 14 15 /** Called when the activity is first created. */ 16 @Override 17 public void onCreate(Bundle savedInstanceState) { 18 super.onCreate(savedInstanceState); 19 setContentView(R.layout.main); 20 21 22 sdCardDir = Environment.getExternalStorageDirectory(); 23 saveFile = new File(sdCardDir,"wms.txt"); 24 25 26 27 ed_text = (EditText) findViewById(R.id.edit_01); 28 text_view = (TextView) findViewById(R.id.text_01); 29 30 button_01 = (Button)findViewById(R.id.button_01); 31 button_01.setOnClickListener(this); 32 33 button_02 = (Button)findViewById(R.id.button_02); 34 button_02.setOnClickListener(this); 35 } 36 37 38 // Button 监听 39 @Override 40 public void onClick(View v) 41 { 42 // TODO Auto-generated method stub 43 switch(v.getId()) 44 { 45 case R.id.button_01: 46 { 47 Toast.makeText(SD_Files_RW.this, "开始写入文件!", Toast.LENGTH_SHORT).show(); 48 Write_Files(); 49 //text_view.setText(ed_text.getText().toString()); 50 }break; 51 case R.id.button_02: 52 { 53 Toast.makeText(SD_Files_RW.this, "开始读出文件!", Toast.LENGTH_SHORT).show(); 54 Read_Files(); 55 56 }break; 57 } 58 59 } 60 61 private void Write_Files() 62 { 63 64 //得到用户输入字符 65 Text_of_input = ed_text.getText().toString(); 66 67 68 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 69 70 71 //创建 初始化 流对象 72 try{ 73 74 outStream = new FileOutputStream(saveFile); 75 } 76 catch (FileNotFoundException e){ 77 Toast.makeText(this, "文件不存在!", Toast.LENGTH_SHORT).show(); 78 return ; 79 } 80 81 82 try { 83 84 //把内容写入文件 85 outStream.write(Text_of_input.getBytes()); 86 87 } catch (FileNotFoundException e) { 88 89 // TODO Auto-generated catch block 90 Toast.makeText(this, "文件未发现异常!", Toast.LENGTH_SHORT).show(); 91 92 } catch (IOException e) { 93 94 // TODO Auto-generated catch block 95 Toast.makeText(this, "文件读写异常!", Toast.LENGTH_SHORT).show(); 96 97 } 98 catch (NullPointerException e){ 99 Toast.makeText(this, "文件读写空指针异常!", Toast.LENGTH_SHORT).show(); 100 } 101 finally { 102 103 try { 104 105 //关闭文件输出流 106 107 108 outStream.close(); 109 110 111 } catch (IOException e) { 112 113 // TODO Auto-generated catch block 114 Toast.makeText(this, "文件读写异常!", Toast.LENGTH_SHORT).show(); 115 116 } 117 118 Toast.makeText(this, "文件写入成功!", Toast.LENGTH_SHORT).show(); 119 120 } 121 122 }else { 123 Toast.makeText(this, "内存卡不存在!", Toast.LENGTH_SHORT).show(); 124 return ; 125 } 126 127 } 128 129 private void Read_Files() 130 { 131 132 int len; 133 134 //然后创建一个字节数组输出流 135 byte[] buffer = new byte[1024]; 136 137 ByteArrayOutputStream ostream = new ByteArrayOutputStream(); 138 139 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 140 141 //创建 初始化 流对象 142 try{ 143 144 inStream = new FileInputStream(saveFile); 145 Log.v("Instance inStream", "实例化inStream!"); 146 } 147 catch (FileNotFoundException e){ 148 Toast.makeText(this, "文件不存在!", Toast.LENGTH_SHORT).show(); 149 return ; 150 } 151 152 153 try{ 154 155 Log.v("Read", "读数据!"); 156 157 while((len=inStream.read(buffer))!=-1){ 158 ostream.write(buffer, 0, len); 159 160 } 161 162 Log.v("Read", "读成功!"); 163 164 }catch (IOException e){ 165 Toast.makeText(this, "读 异常!", Toast.LENGTH_SHORT).show(); 166 } 167 168 169 try{ 170 //最后关闭输入流和输出流 171 inStream.close(); 172 ostream.close(); 173 174 Log.v("Read", "读成功!"); 175 176 }catch (IOException e){ 177 Toast.makeText(this, "读 异常!", Toast.LENGTH_SHORT).show(); 178 } 179 180 String str = new String(ostream.toByteArray()); 181 182 Log.v("Disp", "文本显示!"); 183 184 text_view.setText(str); 185 186 }else{ 187 Toast.makeText(this, "内存卡不存在!", Toast.LENGTH_SHORT).show(); 188 return ; 189 } 190 } 191 192 193 194 }