자바I/O File 복사 함수

황제낙엽 2009.02.08 19:15 조회 수 : 668 추천: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. }  
번호 제목 글쓴이 날짜 조회 수
166 org.apache.commons.fileupload.servlet.ServletFileUpload 를 이용한 파일 업로드 file 황제낙엽 2009.11.19 754
165 Error reading tld listeners java.lang.NullPointerException 황제낙엽 2009.10.14 713
164 Cannot find the tag library descriptor for “http://java.sun.com/jsp/jstl/core 황제낙엽 2009.10.14 1628
163 Transfer-Encoding: chunked VS Content-Length 황제낙엽 2009.09.17 709
162 서블릿 응답 헤더(Response Header) 황제낙엽 2009.09.17 652
161 같은 문자열인데도 정규식에서 해당 문자열을 파싱하지 못하는 경우 황제낙엽 2009.08.08 724
160 MultipartRequest (cos.jar)와 서블릿을 이용한 업로드 file 황제낙엽 2009.06.19 896
159 [대용량 파일 업로드] multipart form parser - http file upload, database 저장 java class 연재2 file 황제낙엽 2009.06.19 2591
158 [대용량 파일 업로드] multipart form parser - http file upload 기능 java class 연재1 file 황제낙엽 2009.06.19 1957
157 [reflection/리플렉션] Class.forName 황제낙엽 2009.05.27 673
156 문자열 내의 공백을 제거하는 간단한 정규식 황제낙엽 2009.05.20 744
155 문자열에서 특수 문자 (Escape Sequence) 처리 file 황제낙엽 2009.02.20 1836
154 정규표현식을 사용하는 String클래스의 replaceAll() 함수 개량 황제낙엽 2009.02.09 884
» File 복사 함수 황제낙엽 2009.02.08 668
152 JSP session 정보 얻기 황제낙엽 2009.01.21 624
151 서버상의 로컬경로 (실제경로) 관련 환경변수 황제낙엽 2009.01.21 904
150 java.net.URL 생성시 로컬 파일에 접근 황제낙엽 2009.01.20 596
149 자바로 구현하는 Web-to-web 프로그래밍 황제낙엽 2009.01.20 657
148 Using Java's Net::URL Class To Access URLs (java.net.URL) 황제낙엽 2009.01.20 932
147 입력받은 문자열의 의미가 숫자인지 단순 텍스트인지 판별해야 할 때 황제낙엽 2009.01.09 772