자바I/O File 복사 함수

황제낙엽 2009.02.08 19:15 조회 수 : 412 추천:212

sitelink1  
sitelink2  
sitelink3  
sitelink4  
sitelink5  
sitelink6  
1. java.io 패키지 이용
·미리보기 | 소스복사·
  1. public void copyFile(File in, File out) throws Exception {   
  2.   File parent = out.getParentFile();   
  3.   if (!parent.exists()) {   
  4.     parent.mkdir();   
  5.   }   
  6.   FileInputStream fis = new FileInputStream(in);   
  7.   FileOutputStream fos = new FileOutputStream(out);   
  8.   byte[] buf = new byte[1024];   
  9.   int i = 0;   
  10.   while ((i = fis.read(buf)) != -1) {   
  11.     fos.write(buf, 0, i);   
  12.   }   
  13.   fis.close();   
  14.   fos.close();   
  15. }  

2. java.nio 패키지 이용
·미리보기 | 소스복사·
  1. public void copyFile(File in, File out) throws Exception {   
  2.   File parent = out.getParentFile();   
  3.   if (!parent.exists()) {   
  4.     parent.mkdir();   
  5.   }        
  6.   FileChannel sourceChannel = new FileInputStream(in).getChannel();   
  7.   FileChannel destinationChannel = new FileOutputStream(out).getChannel();   
  8.   sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);   
  9.   sourceChannel.close();   
  10.   destinationChannel.close();   
  11. }  
번호 제목 글쓴이 날짜 조회 수
163 서블릿 응답 헤더(Response Header) 황제낙엽 2009.09.17 461
162 같은 문자열인데도 정규식에서 해당 문자열을 파싱하지 못하는 경우 황제낙엽 2009.08.08 367
161 MultipartRequest (cos.jar)와 서블릿을 이용한 업로드 file 황제낙엽 2009.06.19 651
160 [대용량 파일 업로드] multipart form parser - http file upload, database 저장 java class 연재2 file 황제낙엽 2009.06.19 2231
159 [대용량 파일 업로드] multipart form parser - http file upload 기능 java class 연재1 file 황제낙엽 2009.06.19 1678
158 [reflection/리플렉션] Class.forName 황제낙엽 2009.05.27 468
157 문자열 내의 공백을 제거하는 간단한 정규식 황제낙엽 2009.05.20 399
156 문자열에서 특수 문자 (Escape Sequence) 처리 file 황제낙엽 2009.02.20 1633
155 정규표현식을 사용하는 String클래스의 replaceAll() 함수 개량 황제낙엽 2009.02.09 508
» File 복사 함수 황제낙엽 2009.02.08 412
153 JSP session 정보 얻기 황제낙엽 2009.01.21 437
152 서버상의 로컬경로 (실제경로) 관련 환경변수 황제낙엽 2009.01.21 634
151 java.net.URL 생성시 로컬 파일에 접근 황제낙엽 2009.01.20 371
150 자바로 구현하는 Web-to-web 프로그래밍 황제낙엽 2009.01.20 425
149 Using Java's Net::URL Class To Access URLs (java.net.URL) 황제낙엽 2009.01.20 581
148 입력받은 문자열의 의미가 숫자인지 단순 텍스트인지 판별해야 할 때 황제낙엽 2009.01.09 373
147 사용팁 황제낙엽 2008.07.24 296
146 문자열 처리 - StringTokenizer 와 String.split() 황제낙엽 2008.07.08 462
145 숫자의 형식화 #1(Part-1)-java.text.NumberFormat 황제낙엽 2008.07.08 296
144 숫자 에 대응 되는 문자의 형식화 #2 황제낙엽 2008.07.08 342