Android测试读写sd卡文件与写sd卡文件耗时

怀抱观古今,寝食展戏谑。这篇文章主要讲述Android测试读写sd卡文件与写sd卡文件耗时相关的知识,希望能为你提供帮助。
测试从sd卡读1k大小的文件,再写1k大小的文件,由于处理耗时很短,所以循环500次,查看耗时;测试写1k大小的文件,直接在内存构造一个1k的buffer,将这个buffer直接写到文件,同样循环500次,查看耗时。
思路:首先读写文件,建立文件输入输出流,然后将读取的数据直接写入文件,打印时间戳,查看耗时;只写文件,新建一个1k的buffer,然后用文件输出流写入文件,打印时间戳,查看耗时。
直接上代码:
【Android测试读写sd卡文件与写sd卡文件耗时】

1 package com.example.ghimtim.myapplication; 2 3 import android.Manifest; 4 import android.content.Context; 5 import android.content.pm.PackageManager; 6 import android.os.Environment; 7 import android.os.SystemClock; 8 import android.support.v4.app.ActivityCompat; 9 import android.support.v4.content.ContextCompat; 10 import android.support.v7.app.AppCompatActivity; 11 import android.os.Bundle; 12 import android.view.View; 13 import android.widget.Button; 14 import android.widget.EditText; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 18 import java.io.File; 19 import java.io.FileInputStream; 20 import java.io.FileOutputStream; 21 import java.io.InputStreamReader; 22 import java.io.OutputStreamWriter; 23 24 public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{ 25 26private static final int MY_PERMISSIONS_REQUEST = 10000; 27 28private Button bt_wr; 29private Button bt_w; 30private TextView tv_wr; 31private TextView tv_w; 32private EditText et_time; 33private File sdcard; 34 35private Context mContext; 36 37int Fortime = 1000; 38char[] buffer = new char[1024]; 39long time; 40 41 42@Override 43protected void onCreate(Bundle savedInstanceState) { 44super.onCreate(savedInstanceState); 45setContentView(R.layout.activity_main); 46mContext = this; 47requestpermisson(); 48sdcard= Environment.getExternalStorageDirectory(); 49initData(); 50initView(); 51} 52 53private void initData() { 54for(int i = 0 ; i < 1024; i++){ 55buffer[i] = ‘b‘; 56} 57} 58 59private void requestpermisson() { 60ActivityCompat.requestPermissions(this, 61new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 62MY_PERMISSIONS_REQUEST); 63 64} 65 66@Override 67public void onRequestPermissionsResult(int requestCode, 68String permissions[], int[] grantResults) { 69switch (requestCode) { 70case MY_PERMISSIONS_REQUEST: { 71// If request is cancelled, the result arrays are empty. 72if (grantResults.length > 0 73& & grantResults[0] == PackageManager.PERMISSION_GRANTED) { 74 75// permission was granted, yay! Do the 76// contacts-related task you need to do. 77 78} else { 79Toast.makeText(this,"no permission!",Toast.LENGTH_LONG).show(); 80 //this.finish(); 81// permission denied, boo! Disable the 82// functionality that depends on this permission. 83} 84return; 85} 86} 87} 88 89private void initView() { 90bt_wr = (Button) findViewById(R.id.write_read_sd_file); 91bt_w = (Button) findViewById(R.id.write_sd_file); 92tv_wr = (TextView) findViewById(R.id.write_read_sd_file_time); 93tv_w = (TextView) findViewById(R.id.write_sd_file_time); 94et_time = (EditText) findViewById(R.id.for_time); 95et_time.setText(String.valueOf(Fortime)); 96bt_wr.setOnClickListener(new View.OnClickListener() { 97@Override 98public void onClick(View v) { 99if(!et_time.getText().toString().equals("")){ 100Fortime =Integer.parseInt(et_time.getText().toString()); 101} 102time = System.currentTimeMillis(); 103writeAndReadForTime(); 104 105} 106}); 107bt_w.setOnClickListener(new View.OnClickListener() { 108@Override 109public void onClick(View v) { 110if(!et_time.getText().toString().equals("")){ 111Fortime =Integer.parseInt(et_time.getText().toString()); 112} 113time = System.currentTimeMillis(); 114ReadFileForTime(); 115} 116}); 117} 118 119private void ReadFileForTime() { 120for(int i =0; i< Fortime; i ++){ 121ReadFile(); 122} 123time = System.currentTimeMillis() - time; 124tv_w.setText(String.valueOf(time)); 125} 126 127private void writeAndReadForTime() { 128for(int i =0; i< Fortime; i ++){ 129writeAndRead(); 130} 131time = System.currentTimeMillis() - time; 132tv_wr.setText(String.valueOf(time)); 133} 134 135private void ReadFile() { 136File file2 =new File(sdcard,"c.txt"); 137if(!sdcard.exists()){ 138return; 139} 140try{ 141 //file2.createNewFile(); 142FileOutputStream fos=new FileOutputStream(file2); 143OutputStreamWriter osw=new OutputStreamWriter(fos); 144String str = new String(buffer); 145osw.write(str); 146osw.flush(); 147osw.close(); 148fos.close(); 149}catch (Exception e){ 150e.printStackTrace(); 151} 152} 153 154private void writeAndRead() { 155File file1 = new File(sdcard,"a.txt"); 156File file2 =new File(sdcard,"b.txt"); 157if(!sdcard.exists()){ 158return; 159} 160try{ 161 //file2.createNewFile(); 162FileInputStream fis = new FileInputStream(file1); 163InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); 164FileOutputStream fos=new FileOutputStream(file2); 165OutputStreamWriter osw=new OutputStreamWriter(fos); 166char[] input =new char[fis.available()]; 167isr.read(input); 168String str = new String(input); 169osw.write(str); 170osw.flush(); 171isr.close(); 172fis.close(); 173osw.close(); 174fos.close(); 175}catch (Exception e){ 176e.printStackTrace(); 177} 178} 179 }

还有一个注意的地方,读写sd卡,在Android6.0以后需要动态申请读写外部存储的权限!
1 < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

(编码时粗心地将uses-permission写进application标签里面= =,这个uses-permission是应该要和application同级的。。。)


    推荐阅读