Java Read file contents three forms and comparison of efficiency

Source: Internet
Author: User

Ioutils.getstringfromreader () read mode is the fastest

InputStream in = null;
String line = "";
Long start=0,end=0;
try {
Start = System.currenttimemillis ();
in = new FileInputStream (New File ("D://1.txt"));
InputStreamReader stream = new InputStreamReader (in, "GBK");
char[] temp = new char[1024];
int count = 0;
while ((count = Stream.read (temp))! =-1) {
Line = line + new String (temp, 0, count);
}
End = System.currenttimemillis ();
System.err.println (line);
System.err.println (End-start);
In.close ();
} catch (FileNotFoundException e) {
SYSTEM.ERR.PRINTLN ("File not Found");
} catch (IOException e) {
SYSTEM.ERR.PRINTLN ("io read inconsistent");
}

try {
Long Start1 = System.currenttimemillis ();
InputStream in1 = new FileInputStream (New File ("D://1.txt"));
BufferedReader br = new BufferedReader (new InputStreamReader (in1, "GBK"));
String s= "";
char[] temp = new char[1024];
int count=0;
while ((Count=br.read (temp))! =-1) {
s + = new String (temp, 0, count);
}
In1.close ();
Br.close ();
Long end1 = System.currenttimemillis ();
System.out.println (s);
System.out.println ((End-start) + "ReadLines" + (END1-START1));
Long Start2=system.currenttimemillis ();
InputStream in2 = new FileInputStream (New File ("D://1.txt"));
String Is=ioutils.getstringfromreader (New BufferedReader (New InputStreamReader (in2, "GBK"));
System.err.println (IS);
Long End2=system.currenttimemillis ();
System.out.println ((End-start) + "ReadLines" + (END1-START1) + "test" + (END2-START2));
} catch (FileNotFoundException e) {
SYSTEM.OUT.PRINTLN ("File not Found");
} catch (IOException e) {
SYSTEM.OUT.PRINTLN ("io read inconsistent");
}

Java Read file contents three forms and comparison of efficiency

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.