2016-09-14 2 views
0

파일과 내용을 복사하기위한 코드를 아래에 만들었습니다.BufferedReader가 파일을 복사하지 않습니다.

static void copyFile(File inFile, String destination) { 
    if (inFile.isFile()) { 
     try { 
      String str = destination + "//" + inFile.getName(); 

      BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile),"UTF-8")); 
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(str), false), "UTF-8")); 

      String line; 
      try { 
       while((line = br.readLine()) != null) { 
         bw.write(line); 
         System.out.println(line); 
       }     
      } catch (IOException ex) { 
        Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (UnsupportedEncodingException ex) { 
      Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } else if(inFile.isDirectory()) { 
     String str = destination + "\\" + inFile.getName(); 
     File newDir = new File(str); 
     newDir.mkdir(); 
     for(File file : inFile.listFiles()) 
      copyFile(file, newDir.getAbsolutePath()); 
    } 
} 

코드는 예상대로 목적지에 파일을 creaes하지만 .txt 파일이 비어 있습니다. while 루프

bw.write(line); 

에 대한 부분은

System.out.println(line); 

작품을 작동하지 않습니다.

+0

()가 새 라인을 폐기하는 시도 할 수 있습니다. 파일의 flush() 또는 close()가 없으면 버퍼링 된 모든 데이터가 디스크에 기록되지 않았습니다 (쓰기 버퍼의 목적입니다) –

답변

1

스트림을 플러시하도록하려면 Writer을 닫아야합니다.

String str = destination + "//" + inFile.getName(); 
// note the paranthesis here, notfing that this is has to be closed after leaving the try block. 
try (
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), "UTF-8")); 
    BufferedWriter bw = new BufferedWriter(
      new OutputStreamWriter(new FileOutputStream(new File(str), false), "UTF-8"))) { 

    String line; 
    try { 
     while ((line = br.readLine()) != null) { 
      bw.write(line); 
      System.out.println(line); 
     } 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} catch (FileNotFoundException ex) { 
    ex.printStackTrace(); 

} catch (UnsupportedEncodingException ex) { 
    ex.printStackTrace(); 

} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

또는 finally 블록 사용 : 그들을 폐쇄하지 당신을 경고해야 추가로 IDE 또는 컴파일러 중 하나를

BufferedWriter bw = null; 
BufferedReader br = null; 
try { 
    String str = destination + "//" + inFile.getName(); 
    br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile), "UTF-8")); 
    bw = new BufferedWriter(
      new OutputStreamWriter(new FileOutputStream(new File(str), false), "UTF-8")); 

    String line; 
    try { 
     while ((line = br.readLine()) != null) { 
      bw.write(line); 
      System.out.println(line); 
     } 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} catch (FileNotFoundException ex) { 
    ex.printStackTrace(); 

} catch (UnsupportedEncodingException ex) { 
    ex.printStackTrace(); 

} finally { 
    try { 
     if(bw != null) 
      bw.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     if (br != null) 
      br.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

을이이 중 하나를 능숙 방법과 새로운 시도 (선호)를 사용하여 수행 할 수 있습니다 .

0

쓰기가 끝나면 bw.flush() 메서드를 호출하는 것을 잊어 버립니다.

while((line = br.readLine()) != null){ 
     bw.write(line); 
     System.out.println( line); 
     }  
bw.flush(); 

버퍼링 된 콜백 메소드를 기억하십시오.

0

당신은 당신이 작성한 Readline 할 때이

FileWriter fw = new FileWriter(inFile.getAbsoluteFile()); 
     BufferedWriter bw = new BufferedWriter(fw); 

      bw.write(line);