2.SD卡讀寫操作
網站建設哪家好,找創(chuàng)新互聯(lián)建站!專注于網頁設計、網站建設、微信開發(fā)、微信小程序開發(fā)、集團企業(yè)網站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了新華免費建站歡迎大家使用!
1讀寫sd卡首先得添加讀寫權限
2.建一個布局文件,用來接收和展示數(shù)據(jù)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/buttonWrite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="寫入數(shù)據(jù)" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <EditText android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/buttonWrite" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:id="@+id/buttonReade" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="讀取數(shù)據(jù)" android:layout_below="@+id/edit" android:layout_alignLeft="@+id/buttonWrite" android:layout_alignStart="@+id/buttonWrite" android:layout_marginTop="73dp" /> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="yu" android:textSize="20sp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="167dp" /> </RelativeLayout>
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{ //如果手機插入了sd卡,而且 具有訪問sd卡的權限 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()); //輸出文件內容 raf.write(content.getBytes()); //關閉 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(); //獲取制定文件對應的輸入流 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)讀取文件內容 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數(shù)據(jù)存儲與IO(二)
鏈接分享:http://m.rwnh.cn/article44/psgghe.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、面包屑導航、App設計、域名注冊、Google、微信公眾號
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)