Java修改CSV后文件被占用无法操作问题求助
CSV编辑后文件操作失败:文件被占用问题解决
问题描述
参考示例实现CSV编辑功能,编辑逻辑正常,但执行Files.copy/move/delete时抛出错误:
The process cannot access the file because it is being used by another process
已使用try-with-resources语句,尝试线程处理无效,怀疑BufferedReader未正确关闭。通过Windows 11资源监视器发现java.exe占用目标CSV文件,仅关闭Java程序后才能正常操作文件。
相关代码
CSV编辑方法
protected static void edit_csv_data(String columname, String new_data) throws IOException { try (FileReader fr = new FileReader(accountfile); BufferedReader reader = new BufferedReader(fr)) { File tempFile = new File(tempfile); BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile)); String currentLine; int index = 10; int line = 0; while ((currentLine = reader.readLine()) != null) { if (line == Userprofile.getUserline()) { currentLine.trim(); String[] data = currentLine.split(","); for (int x = 0; x < data.length; x++) { if (index == x) { writer.write(new_data); } else writer.write(data[x]); if (x < data.length - 1) writer.write(","); } writer.write(System.getProperty("line.separator")); continue; }else { writer.write(currentLine + System.getProperty("line.separator")); } line += 1; } writer.flush(); writer.close(); reader.close(); System.err.println("write fin"); return; } }
更新数据方法
protected static void update_user_data(String columname, String new_data) throws IOException { DataEdit.edit_csv_data(columname,new_data); System.err.println(Files.isWritable(Path.of(accountfile))); Files.copy(Path.of(tempfile), Path.of(accountfile_back),StandardCopyOption.REPLACE_EXISTING); Files.copy(Path.of(accountfile), Path.of(accountfile_back), StandardCopyOption.REPLACE_EXISTING); Files.copy(Path.of(tempfile), Path.of(accountfile),StandardCopyOption.REPLACE_EXISTING); return; }
CSV文件示例
Username,Password,Account_Type,Name,Surname,ID,Email,Picture_name,Ban_status,Attemp_Login_during_Baned,Last_Login admin,1234,admin,Chicken,Little,62001,ASD.l@hotmail.com,picture.jpg,false,0,132
错误信息
java.nio.file.FileSystemException: src\main\java\allaccount\data\Account.csv: The process cannot access the file because it is being used by another process
环境
- IntelliJ IDEA
- Maven构建
- Java 17
- Windows 11
问题原因
- BufferedWriter未通过try-with-resources管理:当前代码中
BufferedWriter仅手动调用close(),若写入过程中抛出异常,writer无法正常关闭,可能引发资源泄漏。 - 冗余的手动资源关闭:在try-with-resources块内手动调用
reader.close(),属于多余操作,且可能掩盖资源管理的潜在问题。 - 其他模块的文件未关闭:
Userprofile.getUserline()方法可能打开了目标CSV文件但未正确关闭,导致文件被持续占用。 - 文件操作顺序错误:
update_user_data中先将临时文件写入备份、再用原文件覆盖备份的逻辑不合理,易触发文件占用冲突。
解决方法
1. 修复资源管理,将BufferedWriter纳入try-with-resources
修改edit_csv_data方法,把BufferedWriter也放到try-with-resources中,确保资源自动关闭:
protected static void edit_csv_data(String columname, String new_data) throws IOException { try (FileReader fr = new FileReader(accountfile); BufferedReader reader = new BufferedReader(fr); BufferedWriter writer = new BufferedWriter(new FileWriter(tempfile))) { String currentLine; int index = 10; int line = 0; while ((currentLine = reader.readLine()) != null) { if (line == Userprofile.getUserline()) { currentLine = currentLine.trim(); // 接收trim结果,原方法未赋值 String[] data = currentLine.split(","); for (int x = 0; x < data.length; x++) { if (index == x) { writer.write(new_data); } else { writer.write(data[x]); } if (x < data.length - 1) { writer.write(","); } } writer.write(System.lineSeparator()); // 用规范方法获取换行符 } else { writer.write(currentLine); writer.write(System.lineSeparator()); } line += 1; } System.err.println("write fin"); } }
2. 检查并修复Userprofile.getUserline()方法
确认该方法中是否存在打开CSV文件后未关闭的情况,所有文件流必须通过try-with-resources管理,避免资源泄漏。
3. 修正文件备份与替换逻辑
调整update_user_data中的操作顺序,先备份原文件,再用临时文件替换原文件:
protected static void update_user_data(String columname, String new_data) throws IOException { DataEdit.edit_csv_data(columname, new_data); Path accountPath = Path.of(accountfile); Path backupPath = Path.of(accountfile_back); Path tempPath = Path.of(tempfile); // 先备份原文件 Files.copy(accountPath, backupPath, StandardCopyOption.REPLACE_EXISTING); // 用临时文件替换原文件 Files.copy(tempPath, accountPath, StandardCopyOption.REPLACE_EXISTING); // 可选:清理临时文件 Files.deleteIfExists(tempPath); }
4. 验证文件占用情况
修改后运行程序,通过Windows资源监视器确认java.exe是否还会持续占用目标CSV文件,若问题仍存在,需排查其他可能打开该文件的代码模块。
内容的提问来源于stack exchange,提问作者ASD1122
相关产品推荐
相关产品推荐

