简体   繁体   中英

BufferedReader does not copy files

I 've made the code below for copying a file and its contents.

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

The code creaes the files at the destination as it should but the .txt files are empty. The part into the while loop

bw.write(line); 

doesn't work

System.out.println(line); 

works.

You need to close your Writer in order to make him flush the stream. this can either be done using the newer try with ressources method (preferred):

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

or using a finally block:

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

additionally either the IDE or the compiler should warn you for not closing them.

you forget call bw.flush() method after write finished;

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

Buffered io remember call flush method;

You can try this

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

          bw.write(line);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM