2016-09-14 6 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ブロックを使用して::

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(); 
    } 
} 

さらにIDEまたはコンパイラのいずれかがそれらを閉じていないためにあなたを警告する必要があり、これは新しいが、ressources方法(推奨)をしようと使用して行うことができますいずれか。

0

書き込み終了後にbw.flush()メソッドを呼び出すことを忘れてしまいます。

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

バッファされたioがコールフラッシュメソッドを覚えている。

0

あなたはのreadLine()を実行するときに、新しい行を破棄している。この

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

      bw.write(line); 
関連する問題