개발/Java

[Java] 파일 복사

hojak99 2017. 9. 8. 17:13
/**
	 * 파일을 복사시키는 메소드
	 * @param originFile
	 * @param copyPath
	 */
	public void fileCopy(File originFile, String copyPath) {
		FileInputStream inputStream = null;
		FileOutputStream outputStream = null;
		
		FileChannel fcin = null;
		FileChannel fcout = null;
		
		File dir = new File(copyPath);
		if(!dir.exists()){
			dir.mkdirs();
		}
		
		try {
			inputStream = new FileInputStream(originFile);
			outputStream = new FileOutputStream(new File(copyPath+"\\"+originFile.getName()));
			
			fcin = inputStream.getChannel();
			fcout = outputStream.getChannel();
			
			long size = fcin.size();
			fcin.transferTo(0, size, fcout);
			
		} catch (FileNotFoundException e) {
			System.err.println("FileNotFoundException");
			e.printStackTrace();
		} catch (IOException e) {
			System.err.println("IOException");
			e.printStackTrace();
		} finally {
			try{
				inputStream.close();
				outputStream.close();
				fcin.close();
				fcout.close();
			}catch(Exception q){
				q.printStackTrace();
			}
		}
	}


반응형