SD卡操作

  1 package com.wms.file2;

  2 

  3 import java.io.ByteArrayOutputStream;

  4 import java.io.File;

  5 import java.io.FileInputStream;

  6 import java.io.FileNotFoundException;

  7 import java.io.FileOutputStream;

  8 import java.io.IOException;

  9 

 10 import android.app.Activity;

 11 import android.os.Bundle;

 12 import android.os.Environment;

 13 import android.util.Log;

 14 import android.view.View;

 15 import android.view.View.OnClickListener;

 16 import android.widget.Button;

 17 import android.widget.EditText;

 18 import android.widget.TextView;

 19 import android.widget.Toast;

 20 

 21 public class SD_Files_RW extends Activity implements OnClickListener{

 22     

 23     private String Text_of_input;

 24     

 25     private Button button_01,button_02;

 26     private EditText ed_text;

 27     private TextView text_view;

 28     

 29     private File sdCardDir;

 30     private File saveFile;

 31     private FileOutputStream outStream;

 32     private FileInputStream inStream;

 33 

 34 

 35     /** Called when the activity is first created. */

 36     @Override

 37     public void onCreate(Bundle savedInstanceState) {

 38         super.onCreate(savedInstanceState);

 39         setContentView(R.layout.main);

 40         

 41         

 42         sdCardDir = Environment.getExternalStorageDirectory();

 43         saveFile = new File(sdCardDir,"wms.txt");

 44         

 45         

 46         

 47         ed_text = (EditText) findViewById(R.id.edit_01);

 48         text_view = (TextView) findViewById(R.id.text_01);

 49         

 50         button_01 = (Button)findViewById(R.id.button_01);

 51         button_01.setOnClickListener(this);

 52         

 53         button_02 = (Button)findViewById(R.id.button_02);

 54         button_02.setOnClickListener(this);

 55     }

 56     

 57     

 58     // Button 监听

 59     @Override

 60     public void onClick(View v)

 61     {

 62         // TODO Auto-generated method stub

 63         switch(v.getId())

 64         {

 65         case R.id.button_01: 

 66             {

 67                 Toast.makeText(SD_Files_RW.this, "开始写入文件!", Toast.LENGTH_SHORT).show();

 68                 Write_Files();

 69                 //text_view.setText(ed_text.getText().toString());

 70             }break;

 71         case R.id.button_02:

 72             {

 73                 Toast.makeText(SD_Files_RW.this, "开始读出文件!", Toast.LENGTH_SHORT).show();

 74                 Read_Files();

 75                 

 76             }break;    

 77         }

 78         

 79     }

 80 

 81     private void Write_Files()

 82     {

 83         

 84         //得到用户输入字符

 85         Text_of_input = ed_text.getText().toString();

 86         

 87         

 88         if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

 89         

 90             

 91         //创建  初始化 流对象

 92         try{

 93             

 94         outStream = new FileOutputStream(saveFile);

 95         }

 96         catch (FileNotFoundException e){

 97             Toast.makeText(this, "文件不存在!", Toast.LENGTH_SHORT).show();

 98             return ;

 99         }

100         

101 

102         try {

103             

104         //把内容写入文件

105         outStream.write(Text_of_input.getBytes());

106 

107         } catch (FileNotFoundException e) {

108             

109         // TODO Auto-generated catch block

110             Toast.makeText(this, "文件未发现异常!", Toast.LENGTH_SHORT).show();

111 

112         } catch (IOException e) {

113 

114         // TODO Auto-generated catch block

115             Toast.makeText(this, "文件读写异常!", Toast.LENGTH_SHORT).show();

116 

117         } 

118         catch (NullPointerException e){

119             Toast.makeText(this, "文件读写空指针异常!", Toast.LENGTH_SHORT).show();

120           }

121         finally {

122     

123         try {

124 

125         //关闭文件输出流

126 

127 

128         outStream.close();

129 

130         

131         } catch (IOException e) {

132 

133         // TODO Auto-generated catch block

134             Toast.makeText(this, "文件读写异常!", Toast.LENGTH_SHORT).show();            

135 

136         }

137         

138         Toast.makeText(this, "文件写入成功!", Toast.LENGTH_SHORT).show();

139         

140         }

141         

142         }else {

143             Toast.makeText(this, "内存卡不存在!", Toast.LENGTH_SHORT).show();

144             return ;

145          }

146 

147     }

148     

149     private void Read_Files()

150     {

151         

152         int len;

153         

154         //然后创建一个字节数组输出流

155         byte[] buffer = new byte[1024];

156         

157         ByteArrayOutputStream ostream = new ByteArrayOutputStream();

158 

159         if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){

160                         

161             //创建  初始化 流对象

162             try{

163                 

164             inStream = new FileInputStream(saveFile);

165             Log.v("Instance inStream", "实例化inStream!");

166             }

167             catch (FileNotFoundException e){

168                 Toast.makeText(this, "文件不存在!", Toast.LENGTH_SHORT).show();

169                 return ;

170             }

171             

172             

173             try{

174                 

175             Log.v("Read", "读数据!");

176                 

177             while((len=inStream.read(buffer))!=-1){

178                 ostream.write(buffer, 0, len);

179 

180             }

181             

182             Log.v("Read", "读成功!");

183             

184             }catch (IOException e){

185                 Toast.makeText(this, "读 异常!", Toast.LENGTH_SHORT).show();

186             }

187             

188             

189             try{

190             //最后关闭输入流和输出流

191             inStream.close();

192             ostream.close();

193             

194             Log.v("Read", "读成功!");

195             

196             }catch (IOException e){

197                 Toast.makeText(this, "读 异常!", Toast.LENGTH_SHORT).show();

198             }

199             

200             String str = new String(ostream.toByteArray());

201             

202             Log.v("Disp", "文本显示!");

203             

204             text_view.setText(str);

205         

206     }else{    

207         Toast.makeText(this, "内存卡不存在!", Toast.LENGTH_SHORT).show();

208         return ;    

209     }

210     }

211 

212     

213 

214 }

权限:

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

你可能感兴趣的:(操作)