Java IO reads files to string

Source: Internet
Author: User
Tags return string tostring stringbuffer
public static String loadAFileToStringDE1 (File f) throws IOException {
Long begintime = System.currenttimemillis ();
InputStream is = null;
String ret = null;
try {
is = new Bufferedinputstream (new FileInputStream (f));
Long contentlength = F.length ();
Bytearrayoutputstream OutStream = new Bytearrayoutputstream (contentlength > 0?) (int) contentlength:1024);
byte[] buffer = new byte[4096];
int Len;
while (len = is.read (buffer)) > 0) {
Outstream.write (buffer, 0, Len);
}
Outstream.close ();
ret = outstream.tostring ();
byte[] ba = Outstream.tobytearray ();
ret = new String (BA);
finally {
if (is!=null) {try{is.close ();} catch (Exception e) {}}
}
Long endtime = System.currenttimemillis ();
System.out.println ("Method 1" + (Endtime-begintime) + "MS");
return ret;
}

public static String loadAFileToStringDE2 (File f) throws IOException {
Long begintime = System.currenttimemillis ();
InputStream is = null;
String ret = null;
try {
is = new FileInputStream (f);
Long contentlength = F.length ();
byte[] ba = new byte[(int) contentlength];
Is.read (BA);
ret = new String (BA);
finally {
if (is!=null) {try{is.close ();} catch (Exception e) {}}
}
Long endtime = System.currenttimemillis ();
System.out.println ("Method 2" + (Endtime-begintime) + "MS");
return ret;
}

public static String loadAFileToStringDE3 (File f) throws IOException {
Long begintime = System.currenttimemillis ();
BufferedReader br = null;
String ret = null;
try {
br = new BufferedReader (new FileReader (f));
String line = null;
StringBuffer sb = new StringBuffer ((int) f.length ());
while (line = Br.readline ())!= null) {
Sb.append (line). append (Line_break);
}
ret = sb.tostring ();
finally {
if (br!=null) {try{br.close ();} catch (Exception e) {}}
}
Long endtime = System.currenttimemillis ();
System.out.println ("Method 3" + (Endtime-begintime) + "MS");
return ret;
}

3 methods to read a file larger than 50M, outofmemery when the JVM parameter is not set, when the-xmx128m is set. Only method 3 can pass, set to-xmx256m when only method 3 can pass, simply set 512M, can be, run time if normal words are generally in the 4~5s




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.