public static void main(String[] args) {
try {
long scannerStart = System.currentTimeMillis();
for (int i = 1; i < 500000; i++) {
scannerMethod();
}
System.out.println("scanner cost:"
+ (System.currentTimeMillis() - scannerStart) + " ms");
long streamStart = System.currentTimeMillis();
for (int i = 1; i < 500000; i++) {
streamMethod();
}
System.out.println("stream cost:"
+ (System.currentTimeMillis() - streamStart) + " ms");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void scannerMethod() throws MalformedURLException, IOException{
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
try( Scanner s = new Scanner(in) ){
s.useDelimiter("\\A").next();
}
in.close();
}
public static void streamMethod() throws IOException{
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
inputStream2String(in);
in.close();
}
public static String inputStream2String(InputStream in) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = in.read()) != -1) {
baos.write(i);
}
return baos.toString();
}
scanner cost:26661 ms
stream cost:18642 ms
scanner cost:25268 ms
stream cost:17991 ms
public static void main(String[] args) throws MalformedURLException, IOException {
long method1 = System.currentTimeMillis();
for(int i =1 ;i<50000;i++){
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
inputStream2String1(in);
in.close();
}
System.out.println("method1 cost:"
+ (System.currentTimeMillis() - method1) + " ms");
long method2 = System.currentTimeMillis();
for(int i =1 ;i<50000;i++){
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
inputStream2String2(in);
in.close();
}
System.out.println("method2 cost:"
+ (System.currentTimeMillis() - method2) + " ms");
long method3 = System.currentTimeMillis();
for(int i =1 ;i<50000;i++){
InputStream in = new URL(
"file:/C:/Users/forgkan/Desktop/readme.txt")
.openStream();
inputStream2String3(in);
in.close();
}
System.out.println("method3 cost:"
+ (System.currentTimeMillis() - method3) + " ms");
}
public static String inputStream2String1(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static String inputStream2String2(InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
public static String inputStream2String3(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1) {
baos.write(i);
}
return baos.toString();
}
method1 cost:3086 ms
method2 cost:2056 ms
method3 cost:1813 ms
method1 cost:2986 ms
method2 cost:2045 ms
method3 cost:2156 ms
method1 cost:2921 ms
method2 cost:2047 ms
method3 cost:1795 ms
method1 cost:2980 ms
method2 cost:2015 ms
method3 cost:1744 ms