Java는 파일을 처리 할 수있는 IO 작동 클래스를 제공하지만 파일을 복사하는 방법은 없습니다. 복사 파일은 중요한 작업입니다. 프로그램이 많은 파일을 처리해야합니다. 그러나 Java 파일에 의해 복제 될 수있는 몇 가지 방법이 있으며 4에서 가장 인기있는 방법은 아래에 나열되어 있습니다.
1. FILESTREAMS로 복사하십시오
이것은 한 파일의 내용을 다른 파일에 복사하는 가장 전형적인 방법입니다. FileInputStream을 사용하여 파일 A의 바이트 A를 읽고 파일을 파일 B로 작성하십시오. 이것이 첫 번째 방법의 코드입니다.
개인 void CopyFileUsingFilestreams (파일 소스, 파일 DEST)는 IOException {outputStream output = inp = 새로운 FileInputStream (dest); [1024] int bytesread;읽기 및 쓰기 작업에 대한 몇 가지 데이터가 보이면 다음은 새로운 방법을 볼 수 있습니다.
2. filechannel을 사용하여 복사하십시오
Java Nio에는 전송 프롬 메소드가 포함되어 있으며 문서에 따라 파일 흐름 카피보다 빠릅니다. 이것은 두 번째 방법의 코드입니다.
Private void CopyFileUsingFileChannels (파일 소스, 파일 DEST) IOException {inputChannel = New FileInputStream () , 0, inputchannel.size ()} inputchannel.close ();3. Commons IO를 사용하여 복사하십시오
Apache Commons IO는 파일 루틸 클래스에 복사 파일 메소드를 제공하며, 하나의 파일을 다른 장소에 복사하는 데 사용할 수 있습니다. Apache Commons FileUtils 클래스를 사용할 때 프로젝트를 사용하는 것이 매우 편리합니다. 기본적 으로이 클래스는 Java Nio Filechannel을 사용합니다. 이것은 세 번째 방법의 코드입니다.
개인 정적 void CopyFileUsingApacheCommonsio (파일 소스, 파일 DEST)는 ioException {fileUtils.copyFile (source, dest);} 던지기4. 파일 클래스 Java7로 복사하십시오
Java 7에 경험이있는 경우 파일 파일의 복사 방법을 사용하여 한 파일에서 다른 파일로 복사 할 수 있음을 알 수 있습니다. 이것은 네 번째 방법의 코드입니다.
Private STATIC void CopyFileUsingJava7files (파일 소스, 파일 DEST)는 ioException {files.copy (source.topath (), dest.topath ());} 던지기5. 테스트
이제이 방법 중 하나가 더 효율적인지 확인하면 각 간단한 프로그램을 사용하기 위해 큰 파일을 복사합니다. 캐시에서 성능을 피하기 위해 4 개의 다른 소스 파일과 4 개의 다른 대상 파일을 사용합니다. 코드를 살펴 보겠습니다.
java.fileoutstream; .nio.file.files; /nikos7//desktop/files/sourcefile1.txt "); file dest = new File ("C : //users/nikos7//desktop/files/destfile1.txt ") Filestreams Long Start.NanoTime을 사용한 복사 파일 ( Long End; CopyFileusingFilestreams (Source, Dest); = 새 파일 ( "c : //users/nikos7//desktop/files//sourcefile2.txt"); source, dest); end = system.nanoTime (); //users//nikos7//desktop//sourcefile3.txt "); dest = new File ("c : //users//nikos7//desktop/files/destfile3.txt "); system.nanoTime () time (); out.println //files//sourcefile4.txt "); dest = new File ("c : //users/nikos7//desktop/files/destfile4.txt "); start = system.nanotime (); sio (source, destry) end = system.out.out.println (Apache Commons에 의해 취한 시간); null; try {new fileInputStream (dest); write (buf, 0, bytesread);}} intice {input.close (); private static void copyfilechalechanenels (파일, 파일 dest)는 ioexception {filechannel inputchannel = null; null; try {putchannel = outPutChannel (); {inputchannel.close (); outputchannel.close ();}} 개인 정적 void copyfileusingjava7files (file dest)는 ioexcepion files.copy (source.topath ()); , File Dest) IoException {fileUtils.copyFile (소스, dest);}}}산출:
FILESTREAMS COPY = 1275723600 타임으로 찍은 시간 복사 COPY = 10449963 타임 JAVA7 파일에 의해 찍은 시간 10808333333333333333333333333333333333333333333333397 IO COPY = 1797167777777777777777777777777777777777777777777777777777777777777333333333333333333333333333332 명까지 APACHE
볼 수 있듯이 파일 채널 복사 대형 파일이 가장 좋은 방법입니다. 더 큰 파일을 처리하면 더 빠른 속도 차이가 나타납니다. 이 예는 예입니다. 예제는 파일을 복사하기위한 Java의 네 가지 다른 방법을 보여줍니다.
위는이 기사의 모든 내용입니다.