One performance test----I/o read/write speed (1)

Source: Internet
Author: User

The company does the intelligent hardware, some places need to interact with the hardware chip, not much to say, directly on the code:

ImportJava.io.File;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportAndroid.os.AsyncTask;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.os.SystemClock;Importandroid.support.v7.app.ActionBarActivity;ImportAndroid.util.Log;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classMainactivityextendsactionbaractivity {PrivateTextView Mwritetime; PrivateButton mbtntest; Private Static FinalString TAG = "Ilejia::testsdcard"; PrivateTestasynctask Mtesttask =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mwritetime=(TextView) Findviewbyid (r.id.write_time); Mbtntest=(Button) Findviewbyid (r.id.btn_test); Mbtntest.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {if(Mtesttask! =NULL&& mtesttask.getstatus ()! =AsyncTask.Status.FINISHED) {return; } mtesttask=NewTestasynctask (); Mtesttask.execute ((void[])NULL);    }        }); }        Private classTestasynctaskextendsAsynctask<void, Void, long>{@Overrideprotected voidOnPreExecute () {mbtntest.setenabled (false); Mwritetime.settext ("Testing ..."); } @OverrideprotectedLong doinbackground (Void ... params) {returnruntest (); }                protected voidOnPostExecute (Long result) {mbtntest.setenabled (true); if(Result > 0) {Mwritetime.settext ("Write 100M Data time consuming" + result + "MS");    }        }; } Longruntest () {File Mediastoragedir=NewFile (Environment.getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM), "Ilejia"); File MediaFile=NewFile (Mediastoragedir.getpath () + File.separator + "Test.dat"); if(!mediastoragedir.exists ()) {            if(!Mediastoragedir.mkdirs ()) {LOG.E (TAG,"Failed to create directory"); return-1; }        }//if (mediafile.exists ()) {//Mediafile.delete ();//        }LOG.I (TAG, "Preparing data"); StringBuilder s=NewStringBuilder (); S.append ("This was a test string writing to file.");  for(inti = 0; i < 1000; i++) {s.append ("0123456789"); } log.i (TAG,"Write Data"); LongStartTime =Systemclock.uptimemillis (); FileOutputStream Stream=NULL; Try{Stream=NewFileOutputStream (MediaFile); byte[] buf =s.tostring (). GetBytes ();  for(inti = 0; I < 10000; ++i)                        {Stream.Write (BUF);          } stream.close (); } Catch(FileNotFoundException e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } finally {            if(Stream! =NULL) {                Try{stream.close (); } Catch(Exception e) {e.printstacktrace (); }}} log.i (TAG,"Write Data Successful"); returnSystemclock.uptimemillis ()-StartTime; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); }}

One performance test----I/o read/write speed (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.