繁体   English   中英

BufferedReader不复制文件

[英]BufferedReader does not copy files

我在下面做了代码,用于复制文件及其内容。

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

该代码将在目标位置创建文件,但.txt文件为空。 进入while循环的部分

bw.write(line); 

不起作用

System.out.println(line); 

作品。

您需要关闭您的Writer才能使他冲洗流。 可以使用较新的try with ressources方法(首选)完成此操作:

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或编译器都应警告您不要关闭它们。

您会在写入完成后忘记调用bw.flush()方法;

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

缓冲的io记得调用flush方法;

你可以试试这个

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

          bw.write(line);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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