启动时间平均时间

来源:互联网 发布:淘宝客链接生成二维码 编辑:程序博客网 时间:2024/06/02 16:51

//加一个参数表示测试次数,测完启动时间,输出每次的值,并求平均值输出

import okio.BufferedSource;import okio.Okio;import java.io.File;import java.io.IOException;import java.nio.file.FileSystem;import java.util.Locale;public class Main {    public static void main(String[] args) {        FileSystem        int total = 0;            //Integer.parseInt(args[0])中,Integer是java中的一个内置类,parseInt()是这个类的一个静态方法,这个方法的作用是把括号里面的参数(args[0])转为int型的值        for (int i = 0; i < Integer.parseInt(args[0]); i++) {            int time = readTime(i);            System.out.println(time);            total += time;        }        System.out.println("平均时间为 "+total/Integer.parseInt(args[0]));    }    private static int readTime(int i) {        File file = new File(String.format(Locale.getDefault(), "%d.txt", i));        BufferedSource source = null;        try {            source = Okio.buffer(Okio.source(file));            String line = source.readUtf8Line();            while (line != null) {                if (line.startsWith("ThisTime")) {                    String time = line.split(":")[1].trim();                    return Integer.parseInt(time);                }                line = source.readUtf8Line();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                if (source != null) {                    source.close();                }            } catch (IOException e) {                e.printStackTrace();            }        }        return 0;    }}