---
title: "关于用读取一个文件，然后删除文件失败的解决办法"
author: "yoscar"
date: "2010-06-16T21:10:12Z"
tags: ["C", "C#", "C++", "F#"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-06-16T21:10:12Z"
id: 692664
url: "https://yoscar.iteye.com/blog/692664"
---

问题描述：
读取本地的一个文件夹（下面的文件），在流关闭前删除的话，是不会成功的。下面给出例子：
	File dataList =  f.listFiles();;
	File f = null;
	f = new File("C:\\Documents and Settings\\Administrator\\桌面\\图片");
         for (int i = 0; i < dataList.length; i++) {

try {
fis = new FileInputStream(dataList*.getAbsolutePath());
filepath = dataList*.getAbsolutePath();
//在这里删除文件是不会成功的
//  File delfile = new File(filepath);
//  boolean b = delfile.delete();
				
} catch (Exception e) {
	e.printStackTrace();
	} finally {
	fis.close();
	//必须在fis关闭之后删除才能成功
         File delfile = new File(filepath);
 	boolean b = delfile.delete();
 	if(b){
 	System.out.println("删除文件成功"); 
 	}else{
 	System.out.println("删除文件失败"); 
 	}          
}