2015-01-05 5 views
1
import java.io.*; 

public class ReadFile { 

    public static void main(String[] args) throws IOException { 

      File in = new File("in.txt"); 
      //File out = new File("out.txt"); 
      FileOutputStream fos= new FileOutputStream("o.txt"); 
      //PrintWriter fw= new PrintWriter(out); 

      if(!in.exists()) 
      { 
       in.createNewFile(); 
       //System.out.println("Hey"); 
      } 

      FileReader is = new FileReader(in); 
      BufferedReader br= new BufferedReader(is); 


      while(true) 
      { 
       if(in.canRead()) 
       { 
        try { 
         System.out.println(br.readLine()); 
         fos.write((br.readLine()).getBytes()); 
        } catch (Exception e) { 
         // TODO Auto-generated catch block 

         fos.close(); 
         br.close(); 

         System.out.println("Im breaking");      
         break; 

        } 
       } 
       else 
       { 
        fos.close();      
        System.out.println("closed"); 
        break; 
       ) 

      } 
    } 
} 
//end of file 
  • 내가 Java.I에서 다른 파일 우리가 바이트로 바이트를 복사 할 수 있습니다 알고에 하나 개의 파일에서 텍스트를 복사하려고하지만, ! 뭐가 문제 야?

실제 파일 : Actual file파일 복사 (전체 파일을 복사하기되지 않음) 내가 문자열을 사용하여 수행 할

출력 파일 : Output file (no formatting and the whole document is not getting copied)

+0

'fos.write (br.readLine() + "\ n");'? – Charlie

답변

0

당신이 Java7 작업하는 경우 당신은 그래서 당신이 파일을 읽을 필요가 없습니다 Files.copy(source.toPath(), dest.toPath());를 사용할 수 있습니다. 또는 apache commons io에서 FileUtils.copyFile(source, dest);을 사용하십시오.

0
    System.out.println(br.readLine()); 
        fos.write((br.readLine()).getBytes()); 

당신

  1. 읽기 한 줄은, 따라서 일부 라인은 출력 파일에서 누락 될 수 있습니다 fos

에 쓰기는

  • 다른 라인을 읽기 stdout에 인쇄 할 수 있습니다. 또한 readLine()은 줄 종결 문자를 삭제합니다. 혼자서 다시 추가해야합니다.