-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferExample.java
More file actions
45 lines (37 loc) ยท 1.46 KB
/
BufferExample.java
File metadata and controls
45 lines (37 loc) ยท 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package ch18.sec07.exam01;
import javax.swing.*;
import java.io.*;
import java.nio.Buffer;
public class BufferExample {
public static void main(String[] args) throws IOException {
String original = BufferExample.class.getResource("originalFile1.jpg").getPath();
String target = "temp/targetFile1.jpg";
FileInputStream fis = new FileInputStream(original);
FileOutputStream fos = new FileOutputStream(target);
String original2 = BufferExample.class.getResource("originalFile1.jpg").getPath();
String target2 = "temp/targetFile2.jpg";
FileInputStream fis2 = new FileInputStream(original2);
FileOutputStream fos2 = new FileOutputStream(target2);
BufferedInputStream bis = new BufferedInputStream(fis2);
BufferedOutputStream bos = new BufferedOutputStream(fos2);
long nonBufferTime = copy(fis, fos);
System.out.println("Buffer X : " + nonBufferTime);
long bufferTime = copy(bis, bos);
System.out.println("Buffer O : " + bufferTime);
fis.close();
fos.close();
bis.close();
bos.close();
}
private static long copy(InputStream is, OutputStream os) throws IOException {
long start = System.nanoTime();
while (true) {
int data = is.read();
if (data == -1) break;
os.write(data);
}
os.flush();
long end = System.nanoTime();
return (end - start);
}
}