FileSystemException甚至在关闭缓冲区之后

时间:2016-03-17 22:14:31

标签: java

我有这个问题java.nio.file.FileSystemException: The process cannot access the file because it is being used by another process.,我无法理解原因。 System.err.println(e.getFile());表示导致异常的文件是groupFile,但我在使用closeBuffers()之前关闭了缓冲区。 我的代码可能出现什么问题?

File groupFile = getFile(_grp +File.separator+ mensagem.getGroup().getName()+".txt");
ServerLogHandler group2SLH = linkHandlerToFile(groupFile);
if(group2SLH.getGroupAdmin().equals(mensagem.getUser().getName())){
    try{
        File temp = createFile(_grp +File.separator+"temp.txt");
        group2SLH.closeBuffers();
        deleteAndWrite(membro2.getName(), groupFile, temp);
        Files.move(temp.toPath(), groupFile.toPath(), REPLACE_EXISTING);
        temp.delete();
    }catch(FileSystemException e){
        System.err.println(e.getFile());
        e.printStackTrace();
    }
}


public void closeBuffers(){
    try {
        this.in.close();
        this.out.flush();
        this.out.close();
    } catch (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}


public static void deleteAndWrite(String deleteThis, File in, File out){
    try {
        BufferedReader in2 = new BufferedReader(new FileReader(in));
        BufferedWriter out2 = new BufferedWriter(new FileWriter(out, true));
        String s;
        StringBuilder sb = new StringBuilder();
        while((s = in2.readLine()) != null){
            if(!s.equals(deleteThis)){
                sb.append(s+System.getProperty("line.separator"));
                out2.write(sb.toString());
            }
        }
        in2.close();
        out2.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案