2.SD卡讀寫操作
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、微信小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了新華免費(fèi)建站歡迎大家使用!
1讀寫sd卡首先得添加讀寫權(quán)限
2.建一個布局文件,用來接收和展示數(shù)據(jù)
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卡寫入數(shù)據(jù)的方法 private void Write(String content){ try{ //如果手機(jī)插入了sd卡,而且 具有訪問sd卡的權(quán)限 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //獲取sd卡的目錄 File sdCradDir=Environment.getExternalStorageDirectory(); File targetFile=new File(sdCradDir.getCanonicalPath()+FILE_NAME); //以指定文件創(chuàng)建RandomAccessFile RandomAccessFile raf=new RandomAccessFile(targetFile,"rw"); //將文件記錄指針移到最后 raf.seek(targetFile.length()); //輸出文件內(nèi)容 raf.write(content.getBytes()); //關(guān)閉 raf.close(); } }catch (Exception E){ E.printStackTrace(); } } //從sd卡讀取數(shù)據(jù) private String Read(){ try { if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ File sdCardDir=Environment.getExternalStorageDirectory(); //獲取制定文件對應(yīng)的輸入流 FileInputStream file=new FileInputStream(sdCardDir.getCanonicalPath()+FILE_NAME); //將制定的輸入流包裝成BufferReader BufferedReader br=new BufferedReader(new InputStreamReader(file)); StringBuilder sb=new StringBuilder(""); String line=null; //循環(huán)讀取文件內(nèi)容 while((line=br.readLine())!=null){ sb.append(line); } //關(guān)閉 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); } }