자바꼼나뭉
60
2021-05-04 18:00:55
1
118

자바 질문드립니다 ㅠㅠ


1GB 파일을 만들고 경로와 파일명을 입력받아 동일하게 복사하는 프로그램을 만들고 있는데 코드가 이상한거 같습니다.. ㅠㅠ 봐주세요  

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;


public class Text {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

FileWriter fout = null;

System.out.println("경로 및 파일명을 입력하시오");

String getName;

String getPath;

getName = scanner.nextLine();

getPath = scanner.nextLine();

String oriFilePath = "C:\\Temp\\system1";

String copyFilePath = "C:\\Temp\\system2";

File oriFile = new File(oriFilePath);

long size = (1024*1024*1024*10);

File copyFile = new File(copyFilePath);

try {

fout = new FileWriter("C:\\Temp\\system1");

while(true) {

String lineString = scanner.nextLine();

if(lineString.length() == 0)

break;

char[] line = null;

fout.write(line,0,lineString.length());

fout.write("\r\n",0,2);

}

fout.close();

}catch (IOException e) {

System.out.println("입출력 오류");

}

scanner.close();

}

}

0
  • 답변 1

  • 종로3가
    117
    2021-05-05 05:34:03

    파일 복사 4가지 방법:

    1. 윈도우 프롬에서 1gb 생성

    fsutil file createnew c:\tmp\1gb_src1.txt 1073741824

    fsutil file createnew c:\tmp\1gb_src2.txt 1073741824

    fsutil file createnew c:\tmp\1gb_src3.txt 1073741824

    fsutil file createnew c:\tmp\1gb_src4.txt 1073741824


    2. javac -cp commons-io-2.4.jar;. Text.java


    3. java -cp commons-io-2.4.jar;. Test

    Java7이 전통적인 스트림보다 거의 3배 빠른걸 알수 있습니다.


    Test.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;
    import java.nio.channels.FileChannel;
    import java.nio.file.Files;
    
    import org.apache.commons.io.FileUtils;
    
    public class Test {
    
        public static void main(String[] args) throws InterruptedException, IOException {
            File source = new File("C:/tmp/1gb_src1.txt");
            File dest = new File("C:/tmp/1gb_dest1.txt");
    
            //copy file conventional way using Stream
            long start = System.nanoTime();
            copyFileUsingStream(source, dest);
            System.out.println("Time taken by Stream  = "+(System.nanoTime()-start));
            
            //copy files using java.nio FileChannel
            source = new File("C:/tmp/1gb_src2.txt");
            dest = new File("C:/tmp/1gb_dest2.txt");
            start = System.nanoTime();
            copyFileUsingChannel(source, dest);
            System.out.println("Time taken by Channel  = "+(System.nanoTime()-start));
            
            //copy files using apache commons io
            source = new File("C:/tmp/1gb_src3.txt");
            dest = new File("C:/tmp/1gb_dest3.txt");
            start = System.nanoTime();
            copyFileUsingApacheCommonsIO(source, dest);
            System.out.println("Time taken by Apache Commons IO  = "+(System.nanoTime()-start));
            
            //using Java 7 Files class
            source = new File("C:/tmp/1gb_src4.txt");
            dest = new File("C:/tmp/1gb_dest4.txt");
            start = System.nanoTime();
            copyFileUsingJava7Files(source, dest);
            System.out.println("Time taken by Java7 Files  = "+(System.nanoTime()-start));        
        }
    
    	private static void copyFileUsingStream(File source, File dest) throws IOException {
    		InputStream is = null;
    		OutputStream os = null;
    		try {
    			is = new FileInputStream(source);
    			os = new FileOutputStream(dest);
    			byte[] buffer = new byte[1024];
    			int length;
    			while ((length = is.read(buffer)) > 0) {
    				os.write(buffer, 0, length);
    			}
    		} finally {
    			is.close();
    			os.close();
    		}
    	}
    
    	private static void copyFileUsingChannel(File source, File dest) throws IOException {
    		FileChannel sourceChannel = null;
    		FileChannel destChannel = null;
    		try {
    			sourceChannel = new FileInputStream(source).getChannel();
    			destChannel = new FileOutputStream(dest).getChannel();
    			destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
    		   }finally{
    			   sourceChannel.close();
    			   destChannel.close();
    	   }
    	}
    
    	private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
    		FileUtils.copyFile(source, dest);
    	}
    
    	private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
    		Files.copy(source.toPath(), dest.toPath());
    	}
    }
    


  • 로그인을 하시면 답변을 등록할 수 있습니다.