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