CORE
HOME > JAVA > J2SE > CORE
2017.04.26 / 20:10

NIO ÀÌ¿ë ÆÄÀÏ Ä«ÇÇ

tomcater
Ãßõ ¼ö 259

º¸Åë °³¹ßÀ» ÇÒ??, ÆÄÀÏ Ä«ÇÇÇÒ ÀÏÀº Á¾Á¾ ÀÖ°í, ´ëºÎºÐ ±× ȸ»ç¿¡ ¸¸µé¾îÁø ¶óÀ̺귯¸®°¡ Àְųª ¾øÀ¸¸é À¥ °Ë»öÇؼ­ °³¹ßÇϴµ¥

 

±âÁ¸¿¡´Â º¸Åë ¹öÆÛ¸¦ ÁöÁ¤ÇÏ°í Ä«ÇǸ¦ ÇÕ´Ï´Ù. ¾È±×·³ °Ì³ª ´À¸®°Åµç¿ä. ƯÈ÷ Å« ÆÄÀÏ °°Àº °æ¿ì´Â¿ä.

 

µû·Î ¹öÆÛ ÁöÁ¤ÇÒ ÇÊ¿ä ¾øÀÌ NIO¸¦ ÀÌ¿ëÇؼ­ Ä«ÇÇ ÇÏ´Â ¼Ò½º ÀÔ´Ï´Ù.

 

 

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileCopyExec {

 public static void main(String[] args) {
  
  String fromFileName = "D:\\ÀÚ·á½Ç\\Disk3.zip";
  String toFileName = "D:\\ÀÚ·á½Ç\\test.zip";
  
  try {
   FileCopyUtil.fileCopy(fromFileName , toFileName);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }
 
}

class FileCopyUtil {
 
 public static void fileCopy(String fromFileName, String toFileName) throws IOException {
  
  FileInputStream fis=null;
  FileOutputStream fos=null;
  FileChannel fcIn=null;
  FileChannel fcOut=null;
  
  try {
   
   //Ä«ÇÇÇÒ ¿øº» ÆÄÀÏÀÇ ½ºÆ®¸³À» ¿­¾î¹ö¸³´Ï´Ù.
   fis = new FileInputStream(fromFileName);
   //Ä«ÇÇÇÒ °÷ÀÇ À§Ä¡ÀÇ(ÆÄÀÏ) ½ºÆ®¸²À» ¿­¾î¹ö¸³´Ï´Ù.
   fos = new FileOutputStream(toFileName);
   
   // ÀԷ½ºÆ®¸² , Ãâ·Â½ºÆ®¸² °¢°¢ÀÇ ÆÄÀÏ Ã¤³ÎÀ» °¡Áö°í ¿É´Ï´Ù.
   fcIn = fis.getChannel();
   fcOut = fos.getChannel();
   
   //ÀԷ½ºÆ®¸²À¸·Î ºÎÅÍ °¡Á®¿Â ÆÄÀÏä³Î¿¡¼­ ÀÌ ¸Þ¼Òµå¸¦ È£ÃâÇϸé , Áö°¡ ¾Ë¾Æ¼­ ¹öÆÛÇØ°¡Áö°í Ä«ÇÇÇعö¸³´Ï´Ù. 
   fcIn.transferTo(0, fcIn.size(), fcOut);
   
  } catch (IOException e) {
   
   throw e;
    
  }finally {
   
   if(fcOut!=null) fcOut.close();
   if(fcIn!=null) fcIn.close();
   if(fos!=null) fos.close();
   if(fis!=null) fis.close();
   
  }
 }
 

 

ÇÙ½ÉÀº  fcIn.transferTo(0, fcIn.size(), fcOut);  ÀÌ ¸Þ¼Òµå È£Ãâ ÀÔ´Ï´Ù.

Áö°¡ ¾Ë¾Æ¼­ ´Ù ÇØÁÝ´Ï´Ù.

 

ù¹ø° ÀÎÀÚ´Â ½ÃÀÛÀ§Ä¡ ÀÔ´Ï´Ù.

µÎ¹ø° ÀÎÀÚ´Â »çÀÌÁî ÀÔ´Ï´Ù.

¼¼¹ø° ÀÎÀÚ´Â Ãâ·ÂµÉ ÆÄÀÏä³ÎÀÔ´Ï´Ù.

 

´õ ÁÁÀº ¹æ¹ýÀÌ ÀÖ´Ù¸é ¾Ë·ÁÁÖ¼¼¿ä.

 

 

¾Æ·¡´Â transferTo() ¸Þ¼ÒµåÀÇ »ó¼¼ÀÔ´Ï´Ù.

 

Transfers bytes from this channel's file to the given writable byte channel.

An attempt is made to read up to count bytes starting at the given position in this channel's file and write them to the target channel. An invocation of this method may or may not transfer all of the requested bytes; whether or not it does so depends upon the natures and states of the channels. Fewer than the requested number of bytes are transferred if this channel's file contains fewer than count bytes starting at the given position, or if the target channel is non-blocking and it has fewer than count bytes free in its output buffer.

This method does not modify this channel's position. If the given position is greater than the file's current size then no bytes are transferred. If the target channel has a position then bytes are written starting at that position and then the position is incremented by the number of bytes written.

This method is potentially much more efficient than a simple loop that reads from this channel and writes to the target channel. Many operating systems can transfer bytes directly from the filesystem cache to the target channel without actually copying them.



Ãâó: http://kimyhcj.tistory.com/56 [¾ÆÀ²¾ÆºüÀÇ ½ºÅ丮]