Android数据存储与IO(二)
2.SD卡读写操作
网站建设哪家好,找创新互联建站!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了新华免费建站欢迎大家使用!
1读写sd卡首先得添加读写权限
2.建一个布局文件,用来接收和展示数据
3.
package xiaocool.net.sdtest; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.RandomAccessFile; public class MainActivity extends ActionBarActivity { private final String FILE_NAME="/xiaocool.bin"; private Button write,read; private EditText editText; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); write=(Button)this.findViewById(R.id.buttonWrite); read=(Button)this.findViewById(R.id.buttonReade); editText=(EditText)this.findViewById(R.id.edit); textView=(TextView)this.findViewById(R.id.textview); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Write(editText.getText().toString()); editText.setText(""); } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { textView.setText(Read()); } }); } //往sd卡写入数据的方法 private void Write(String content){ try{ //如果手机插入了sd卡,而且 具有访问sd卡的权限 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //获取sd卡的目录 File sdCradDir=Environment.getExternalStorageDirectory(); File targetFile=new File(sdCradDir.getCanonicalPath()+FILE_NAME); //以指定文件创建RandomAccessFile RandomAccessFile raf=new RandomAccessFile(targetFile,"rw"); //将文件记录指针移到最后 raf.seek(targetFile.length()); //输出文件内容 raf.write(content.getBytes()); //关闭 raf.close(); } }catch (Exception E){ E.printStackTrace(); } } //从sd卡读取数据 private String Read(){ try { if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File sdCardDir=Environment.getExternalStorageDirectory(); //获取制定文件对应的输入流 FileInputStream file=new FileInputStream(sdCardDir.getCanonicalPath()+FILE_NAME); //将制定的输入流包装成BufferReader BufferedReader br=new BufferedReader(new InputStreamReader(file)); StringBuilder sb=new StringBuilder(""); String line=null; //循环读取文件内容 while((line=br.readLine())!=null){ sb.append(line); } //关闭 br.close(); return sb.toString(); } }catch (Exception E){ E.printStackTrace(); } return null; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
当前名称:Android数据存储与IO(二)
URL网址:http://ybzwz.com/article/psgghe.html