본문 바로가기

개발/JAVA

파일 복사 소스

반응형
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyDir
{
	public static void copyDirectory( File sourcelocation, File targetdirectory ) throws IOException
	{
		// 디렉토리인 경우
		if ( sourcelocation.isDirectory() )
		{
			// 복사될 Directory가 없으면 만듭니다.
			if ( !targetdirectory.exists() )
			{
				targetdirectory.mkdir();
			}

			String[] children = sourcelocation.list();

			for ( int i = 0; i < children.length; i++ )
			{
				copyDirectory( new File( sourcelocation, children[i] ), new File( targetdirectory, children[i] ) );
			}
		}
		else
		{
			// 파일인 경우
			InputStream in = new FileInputStream( sourcelocation );
			OutputStream out = new FileOutputStream( targetdirectory );

			byte[] buf = new byte[1024];
			int len;

			while ( ( len = in.read( buf ) ) > 0 )
			{
				out.write( buf, 0, len );
			}

			in.close();
			out.close();
		}
	}

	public static void main( String[] args ) throws IOException
	{
		File source = new File( "c:\\LOG" );
		File target = new File( "c:\\Temp" );
		copyDirectory( source, target );
	}
}

 

반응형

자바스크립트를 허용해주세요.
[ 자바스크립트 활성화 방법 ]
from Mohon Aktifkan Javascript!