Java BufferedReader从未关闭,但文件可以删除

Java BufferedReader从未关闭,但文件可以删除,java,file,bufferedreader,java-io,Java,File,Bufferedreader,Java Io,最近,我检查了我们的应用程序代码,发现代码中有一个问题 /** * truncate cat tree(s) from the import file */ private void truncateCatTreesInFile(File file, String userImplCode) throws Exception { String rowStr = null, treeCode = null; BufferedReader reader = new Buff

最近,我检查了我们的应用程序代码,发现代码中有一个问题

/**
 * truncate cat tree(s) from the import file
 */
private void truncateCatTreesInFile(File file, String userImplCode) throws Exception
 {
     String rowStr = null, treeCode = null;
     BufferedReader reader = new BufferedReader(new FileReader(file));
     rowStr = reader.readLine(); // skip 1st row - header
     Impl impl;
     List<String> row = null;
     Set<String> truncatedTrees = new HashSet<String>();
     while ((rowStr = reader.readLine()) != null)
     {
         row = CrudServiceHelper.getRowFromFile(rowStr);
         if (row == null) continue;

         impl = getCatImportImpl(row.get(ECatTreeExportImportData.IMPL.getIndex()), userImplCode);
         treeCode = row.get(ECatTreeExportImportData.TREE_CODE.getIndex());
         if(truncatedTrees.contains(treeCode)) continue;

         truncatedTrees.add(treeCode);             
         CatTree catTree = _treeDao.findByCodeAndImpl(treeCode, impl.getId());
         if(catTree!= null) _treeDao.makeTransient(catTree);             
     }
     _treeDao.flush();   
 }
呃]-

基本上,我要做的是从浏览器上传一个文件,并基于该文件生成sql以将数据插入数据库。完成所有操作后,删除该文件

我很惊讶这段代码运行得很好,这里有人有想法吗?我试着用谷歌搜索它,但我一点也不知道

谢谢,
杰克不关闭读卡器可能会导致资源泄漏。删除一个打开的文件可能仍然很好

在Linux(和其他Unix变体)下,如果只是从文件中取消名称链接,则删除文件。一个没有名字的文件实际上被释放了。因此,打开一个文件,删除它(删除它的名称),然后读写它是获得临时文件的一种众所周知的方法。文件关闭后,将释放空间,但不会更早

在Windows下,某些程序会锁定它们读取的文件,这会阻止其他进程删除此类文件。但并非所有的程序都这样做。我没有一台Windows机器来实际测试Java如何处理这个问题


代码没有崩溃并不意味着代码工作完全正确。如果应用程序由于泄漏而消耗越来越多的RAM,那么您注意到的问题可能会在很久以后才显现出来。不过,这不太可能:垃圾收集器最终会关闭读卡器,而且可能很快就会关闭,因为
读卡器
是本地的,不会泄漏出方法。

我在本地windows操作系统中测试了它,这就是为什么我感到惊讶,因为我知道如果有程序在windows中打开文件,如果其他程序试图删除此文件,则将失败。我认为这可能是因为tomcat会在servlet发布后释放所有资源(我的意思是请求完成),并且tomcat还会自动删除临时文件。
 javax.servlet.context.tempdir>
 [java] 2013-03-27 17:45:54,285 INFO [org.apache.struts2.dispatcher.Dispatch