Я создал свой собственный тест для ввода / вывода, но я не могу понять, реально это или нет.
У вас есть пример кода, чтобы показать мне о производительности ввода / вывода?
Есть предложения по его оптимизации?
Заранее спасибо:)
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Random;
import android.content.Context;
import android.os.Environment;
public class MainHandler {
protected Context mContext;
public String Data = new String("");
/** Called when the activity is first created.
* @throws Exception */
public MainHandler(Context c) throws Exception{
this.mContext = c;
generateString();
writeOnHd();
readOnHd();
writeOnSd();
readOnSd();
}
public void writeOnHd() throws Exception {
System.out.println("-------------------------------------------------------");
long startTime = System.currentTimeMillis();
FileOutputStream fOut = mContext.openFileOutput("samplefile.txt", 777);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
int c = 2000;
for (int i = 0; i < c; i++) {
osw.write(this.Data);
osw.flush();
}
osw.close();
System.out.println("Total time to write: "
+ ((System.currentTimeMillis() - startTime))
+ " miliseconds ");
System.gc();
}
public void readOnHd() throws Exception {
long startTime = System.currentTimeMillis();
FileInputStream fIn = mContext.openFileInput("samplefile.txt");
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[1024];
int len = 0;
int length= 1024;
while((len = isr.read(inputBuffer)) != -1){
//System.out.println("Total bytes read: " + len);
}
fIn.close();
System.out.println("Total time to read: "
+ ((System.currentTimeMillis() - startTime))
+ " miliseconds number block ");
mContext.deleteFile("samplefile.txt");
System.gc();
}
public void writeOnSd (){
long startTime = System.currentTimeMillis();
File sd = Environment.getExternalStorageDirectory();
File f = new File(sd, "provafile.txt");
FileWriter fw = null;
BufferedWriter bw = null;
try{
fw = new FileWriter(f, true);
bw = new BufferedWriter(fw);
int c = 2000;
for (int i = 0; i < c; i++) {
bw.write(this.Data);
bw.flush();
}
bw.close();
fw.close();
}
catch (IOException e) {
e.printStackTrace();
}
System.gc();
System.out.println("Total time to write on sd: "
+ ((System.currentTimeMillis() - startTime))
+ " miliseconds ");
}
public void readOnSd(){
long startTime = System.currentTimeMillis();
try{
File f = new File(Environment.getExternalStorageDirectory()+"/provafile.txt");
FileInputStream fIn = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fIn);
char[] inputBuffer = new char[1024];
int len = 0;
int length= 1024;
while((len = isr.read(inputBuffer)) != -1){
//System.out.println("Total bytes read on sd: " + len);
}
fIn.close();
f.delete();
}catch(Exception e){
e.printStackTrace();
}
System.gc();
System.out.println("Total time to read on sd: " +
((System.currentTimeMillis() - startTime)) +
" miliseconds ");
}
public void generateString(){
int c = 2000;
for(int i =0; i<=c; i++) {
Random randomGenerator = new Random();
String container[]= {"a", "D","£","&","e","f","ç","h","§","j","*","]","{","n","o","|",")","4","s","t","u","=","z"};
int n = randomGenerator.nextInt(22);
this.Data += container[n];
}
System.gc();
}
}