Java FileFilter file filter and ReadLine read Line operation instance Code _java

Source: Internet
Author: User

Copy Code code as follows:



Package Com.cjonline.foundation.evisa;

Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileFilter;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import Java.math.BigDecimal;

public class Test {





public static void Main (string[] args) throws Exception {


File filter, file path can use D:\\presstest\\test absolute path, also can use Src/test.


file[] files = new File ("src"). Listfiles (New FileFilter () {


Public Boolean accept (File arg0) {


if (Arg0.getname (). EndsWith (". txt")) {//select TXT file


return true;


}


return false;


}


});


FileInputStream is =null; Input stream Read file


BufferedReader Dr =null; Read rows


for (File file:files) {


System.out.println ("---------" file name: "+ file.getname () +" "----------");


is =new fileinputstream (file);


Dr=new BufferedReader (New InputStreamReader (IS));


String[] strings = new string[]{"Total transferred:", "Requests/Second:", "[Ms] (mean)", "Time per request:",


"Transfer Rate:", "Failed requests:", "Write Errors:"};


bigdecimal[] Bigdecimals = calpress (DR);


int i=0;


for (BigDecimal bigdecimal:bigdecimals) {


System.out.println (strings[i]+ "" +bigdecimal);


i++;


}


System.out.println ();


}


Dr.close ();


Is.close ();


}

private static bigdecimal[] calpress (BufferedReader DR)


Throws IOException {


bigdecimal[] res = new Bigdecimal[]{bigdecimal.zero,bigdecimal.zero,bigdecimal.zero,bigdecimal.zero


, Bigdecimal.zero,bigdecimal.zero,bigdecimal.zero};


String Totaltrans;


while ((Totaltrans = Dr.readline ())!= null) {


if (Totaltrans.startswith ("Total transferred:")) {


string[] st = Totaltrans.split ("");


BigDecimal value = bigdecimal.valueof (double.valueof (st[st.length-2));


Res[0]=res[0].add (value);


}


if (Totaltrans.startswith ("Requests per Second:")) {


string[] st = Totaltrans.split ("");


BigDecimal value = bigdecimal.valueof (double.valueof (st[st.length-3));


Res[1]=res[1].add (value);


}


if (Totaltrans.endswith ("[MS] (mean)")) {


string[] st = Totaltrans.split ("");


BigDecimal value = bigdecimal.valueof (double.valueof (st[st.length-3));


Res[2]=res[2].add (value);


}


if (Totaltrans.startswith ("Time per Request:") &&!totaltrans.endswith ("[MS] (mean)")) {


string[] st = Totaltrans.split ("");


BigDecimal value = bigdecimal.valueof (double.valueof (st[st.length-7));


Res[3]=res[3].add (value);


}


if (Totaltrans.startswith ("Transfer rate:")) {


string[] st = Totaltrans.split ("");


BigDecimal value = bigdecimal.valueof (double.valueof (st[st.length-3));


Res[4]=res[4].add (value);


}


if (Totaltrans.startswith ("Failed requests:")) {


string[] st = Totaltrans.split ("");


BigDecimal value = bigdecimal.valueof (double.valueof (st[st.length-1));


Res[5]=res[5].add (value);


}


if (Totaltrans.startswith ("Write Errors:")) {


string[] st = Totaltrans.split ("");


BigDecimal value = bigdecimal.valueof (double.valueof (st[st.length-1));


Res[6]=res[6].add (value);


}

}
return res;
}
}

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.