import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileStreamArrayTest { public static void main(String[] args) { FileInputStream fi = null; FileOutputStream fo = null; try { //1. 연결 fi = new FileInputStream("D:\\Desert.jpg");//연결 fo = new FileOutputStream("D:\\Desert3.jpg");//덮어쓰기 //fo=new FileOutputStream("D:\\Desert2.jpg",true);//이어쓰기 파일크기가 2배가 되어버린다. //2. I/O작업(읽는 작업, 출력작업) long l1 = System.currentTimeMillis(); int count =0; byte[] buff = new byte[10000];//읽은 데이터는 배열에 있다. int length = fi.read(buff);//몇바이트 읽었는지 while(length != -1){ fo.write(buff,0,length);// 0번부터 length까지 쓰게 된다. 그냥 buff로 쓰면 기존에 있던 바이트들이 중복처리되서 파일 용량이 이전것보다 크게 써지게 된다.
length =fi.read(buff);// count++; } long l2 = System.currentTimeMillis(); System.out.println(l2-l1+"초"); System.out.println("반복횟수 : "+count); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(fi!=null){//만약 fi가 null이 아닐 경우 try { fi.close(); } catch (IOException e) { e.printStackTrace(); } } if(fo!=null){ try { fo.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
<파일을 읽어서 출력해주기 : 파일 복사>
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class FileStreamTest { public static void main(String[] args) { FileInputStream fi = null; FileOutputStream fo = null; try { //1. 연결 fi = new FileInputStream("D:\\Desert.jpg");//연결 //fo = new FileOutputStream("D:\\Desert2.jpg");//덮어쓰기 fo=new FileOutputStream("D:\\Desert2.jpg",true);//이어쓰기 파일크기가 2배가 되어버린다. 만약 텍스트파일을 이어쓰기 했을 경우 텍스트 파일이 복사된다. //2. I/O작업(읽는 작업, 출력작업) int data = fi.read();//1바이트, 여기서 먼저 1바이트 읽게 된다. int count =0; long l1 = System.currentTimeMillis();//시간 계산 while(data!=-1){ fo.write(data);//출력 count++; data = fi.read();//입력 } long l2 = System.currentTimeMillis();//시간 계산 System.out.println("읽은 횟수 : "+count); System.out.println((l2-l1)/1000+"초"); //3. 연결 끊기 //fi.close();//연결 끊기 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(fi!=null){//만약 fi가 null이 아닐 경우 try { fi.close(); } catch (IOException e) { e.printStackTrace(); } } if(fo!=null){ try { fo.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
파일을 byte하나씩 받기
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileStreamTest {
public static void main(String[] args) {
FileInputStream fi = null;
try {
//1. 연결
fi = new FileInputStream("D:\\Desert.jpg");//연결
//2. I/O작업(읽는 작업)
int data = fi.read();//1바이트
int count =0;
long l1 = System.currentTimeMillis();//시간 계산
while(data!=-1){
count++;
data = fi.read();
}
long l2 = System.currentTimeMillis();//시간 계산
System.out.println("읽은 횟수 : "+count);
System.out.println((l2-l1)/1000+"초");
//3. 연결 끊기
//fi.close();//연결 끊기
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fi!=null){//만약 fi가 null이 아닐 경우
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}