public class FileCopyTest {
public static void main(String[] args) {
long millisecond = 0;
try(FileInputStream fis = new FileInputStream("a.zip");
FileOutputStream fos = new FileOutputStream("copy.zip")) {
millisecond = System.currentTimeMillis();
int i;
while((i=fis.read()) != -1) {
fos.write(i);
}
millisecond = System.currentTimeMillis() - millisecond;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println((millisecond) + " 소요되었습니다.");
}
}
/* ** 출력 ** */
// 613 소요되었습니다.
Buffer를 사용하면 1번 예제보다 속도가 훨씬 빠르다.
public class FileCopyTest2 {
public static void main(String[] args) {
long millisecond = 0;
try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.zip")); //버퍼링되어 더 빨라짐
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.zip"))) { //2중 보조스트림
millisecond = System.currentTimeMillis();
int i;
while((i=bis.read()) != -1) {
bos.write(i);
}
millisecond = System.currentTimeMillis() - millisecond;
} catch (IOException e) {
e.printStackTrace();
}
System.out.println((millisecond) + " 소요되었습니다.");
}
}
/* ** 출력 ** */
// 6 소요되었습니다.
파일의 특정 포인터로 이동하고 싶을 때 사용하면 된다.
public class RandomAccessFileTest {
public static void main(String[] args) throws IOException {
RandomAccessFile rf = new RandomAccessFile("random.txt", "rw"); //random.txt 생성
rf.writeInt(100);
System.out.println("pos : " + rf.getFilePointer()); //Int 4Byte이므로 4 출력
rf.writeDouble(3.14);
System.out.println("pos : " + rf.getFilePointer()); //double 8Byte이므로 4+8=12 출력
rf.writeUTF("안녕하세요");
System.out.println("pos : " + rf.getFilePointer()); //String은 맨 뒤 \n 라인있으므로 3Byte*5 + 2 = 17 해서 12+17=29 출력
rf.seek(0); //filePointer 0으로 이동
int i = rf.readInt();
System.out.println(i);
System.out.println("pos : " + rf.getFilePointer()); //Int 4Byte이므로 4 출력
double d = rf.readDouble();
System.out.println(d);
System.out.println("pos : " + rf.getFilePointer()); ///double 8Byte이므로 4+8=12 출력
String str = rf.readUTF();
System.out.println(str);
System.out.println("pos : " + rf.getFilePointer()); //String은 맨 뒤 \n 라인있으므로 3Byte*5 + 2 = 17 해서 12+17=29 출력
}
}