Android测试读写sd卡文件与写sd卡文件耗时
测试从sd卡读1k大小的文件,再写1k大小的文件,由于处理耗时很短,所以循环500次,查看耗时;测试写1k大小的文件,直接在内存构造一个1k的buffer,将这个buffer直接写到文件,同样循环500次,查看耗时。
思路:首先读写文件,建立文件输入输出流,然后将读取的数据直接写入文件,打印时间戳,查看耗时;只写文件,新建一个1k的buffer,然后用文件输出流写入文件,打印时间戳,查看耗时。
直接上代码:
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 26 private static final int MY_PERMISSIONS_REQUEST = 10000; 27 28 private Button bt_wr; 29 private Button bt_w; 30 private TextView tv_wr; 31 private TextView tv_w; 32 private EditText et_time; 33 private File sdcard; 34 35 private Context mContext; 36 37 int Fortime = 1000; 38 char[] buffer = new char[1024]; 39 long time; 40 41 42 @Override 43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.activity_main); 46 mContext = this; 47 requestpermisson(); 48 sdcard= Environment.getExternalStorageDirectory(); 49 initData(); 50 initView(); 51 } 52 53 private void initData() { 54 for(int i = 0 ; i < 1024;i++){ 55 buffer[i] = 'b'; 56 } 57 } 58 59 private void requestpermisson() { 60 ActivityCompat.requestPermissions(this, 61 new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 62 MY_PERMISSIONS_REQUEST); 63 64 } 65 66 @Override 67 public void onRequestPermissionsResult(int requestCode, 68 String permissions[], int[] grantResults) { 69 switch (requestCode) { 70 case MY_PERMISSIONS_REQUEST: { 71 // If request is cancelled, the result arrays are empty. 72 if (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 { 79 Toast.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 } 84 return; 85 } 86 } 87 } 88 89 private void initView() { 90 bt_wr = (Button) findViewById(R.id.write_read_sd_file); 91 bt_w = (Button) findViewById(R.id.write_sd_file); 92 tv_wr = (TextView) findViewById(R.id.write_read_sd_file_time); 93 tv_w = (TextView) findViewById(R.id.write_sd_file_time); 94 et_time = (EditText) findViewById(R.id.for_time); 95 et_time.setText(String.valueOf(Fortime)); 96 bt_wr.setOnClickListener(new View.OnClickListener() { 97 @Override 98 public void onClick(View v) { 99 if(!et_time.getText().toString().equals("")){ 100 Fortime = Integer.parseInt(et_time.getText().toString()); 101 } 102 time = System.currentTimeMillis(); 103 writeAndReadForTime(); 104 105 } 106 }); 107 bt_w.setOnClickListener(new View.OnClickListener() { 108 @Override 109 public void onClick(View v) { 110 if(!et_time.getText().toString().equals("")){ 111 Fortime = Integer.parseInt(et_time.getText().toString()); 112 } 113 time = System.currentTimeMillis(); 114 ReadFileForTime(); 115 } 116 }); 117 } 118 119 private void ReadFileForTime() { 120 for(int i =0;i<Fortime;i ++){ 121 ReadFile(); 122 } 123 time = System.currentTimeMillis() - time; 124 tv_w.setText(String.valueOf(time)); 125 } 126 127 private void writeAndReadForTime() { 128 for(int i =0;i<Fortime;i ++){ 129 writeAndRead(); 130 } 131 time = System.currentTimeMillis() - time; 132 tv_wr.setText(String.valueOf(time)); 133 } 134 135 private void ReadFile() { 136 File file2 = new File(sdcard,"c.txt"); 137 if(!sdcard.exists()){ 138 return; 139 } 140 try{ 141 // file2.createNewFile(); 142 FileOutputStream fos=new FileOutputStream(file2); 143 OutputStreamWriter osw=new OutputStreamWriter(fos); 144 String str = new String(buffer); 145 osw.write(str); 146 osw.flush(); 147 osw.close(); 148 fos.close(); 149 }catch (Exception e){ 150 e.printStackTrace(); 151 } 152 } 153 154 private void writeAndRead() { 155 File file1 = new File(sdcard,"a.txt"); 156 File file2 = new File(sdcard,"b.txt"); 157 if(!sdcard.exists()){ 158 return; 159 } 160 try{ 161 // file2.createNewFile(); 162 FileInputStream fis = new FileInputStream(file1); 163 InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); 164 FileOutputStream fos=new FileOutputStream(file2); 165 OutputStreamWriter osw=new OutputStreamWriter(fos); 166 char[] input = new char[fis.available()]; 167 isr.read(input); 168 String str = new String(input); 169 osw.write(str); 170 osw.flush(); 171 isr.close(); 172 fis.close(); 173 osw.close(); 174 fos.close(); 175 }catch (Exception e){ 176 e.printStackTrace(); 177 } 178 } 179 }
还有一个注意的地方,读写sd卡,在Android6.0以后需要动态申请读写外部存储的权限!
1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
(编码时粗心地将uses-permission写进application标签里面= =,这个uses-permission是应该要和application同级的。。。)