为什么同样的一段程序在java里生成的文件能删除,但是在jsp里运行生成的文件就删除不了?

manbeing3 manbeing3 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
下面是这段程序,它生成一个log.txt文件,如果用java生成就能删除,但是如果用jsp生成,有时候能删除有时候不能。不能删除的时候,给出的错误信息是:文件正在被使用,constraint 不能被violate. 另外这段程序写在了好几个方法里成生不同的logfile。


public static void generatefile(String path,int count) throws Exception{
flag=0;
File f =new File(path);
File[] ff=f.listFiles();
for(int ii=0;ii<ff.length;ii++){
String name=ff[ii].getName();
if (ff[ii].isFile() && name.startsWith("log") && name.endsWith(".txt")){
flag=1;
}
}

if(flag==0) {
String probuffer;
probuffer="here is the logfile";
for (int j=1;j<=count;j++){
probuffer=probuffer+"count"+j;
probuffer=probuffer+".txt\r\n";
}

OutputStream outprof1=new FileOutputStream(path+"log"+".txt");
outprof1.close();
FileWriter fwprof = new FileWriter(path+"log"+".txt");
PrintWriter outprof=new PrintWriter(fwprof);
outprof.write(probuffer);
outprof.println();
outprof.flush();
outprof.close();

}

else {
String[] prof=new String[50];
String probuffer="";
String s=null;
int fileposition1;
int fileposition2;
int account=0;
fileposition1=0;
fileposition2=0;
for(int i=0;i<50;i++)
prof[i]="";

FileReader fr=new FileReader(path+"log.txt");
BufferedReader fff=new BufferedReader(fr);
while((s=fff.readLine())!=null){
if(!s.equals("")){
prof[account]=s.trim();
account=account+1;
}
}

for(int i=0;i<account;i++) {
if(prof[i].equals("<files>"))
fileposition1=i;
if(prof[i].equals("</files>"))
fileposition2=i;
}

for(int i=0;i<fileposition1+1;i++)
probuffer=probuffer+prof[i]+"\r\n";
for(int j=1;j<=count;j++){
probuffer=probuffer+"count"+j;
probuffer=probuffer+".txt\r\n";
}

for(int i=fileposition2;i<50;i++)
probuffer=probuffer+prof[i]+"\r\n";

OutputStream outprof1=new FileOutputStream(path+"log"+".txt");
outprof1.close();
FileWriter fwprof = new FileWriter(path+"log"+".txt");
PrintWriter outprof=new PrintWriter(fwprof);
outprof.write(probuffer);
outprof.println();
outprof.flush();
outprof.close();


}


}


written by Ji Li
...全文
评论
收藏 已收藏
分享
返回顶部
manbeing3
Java SE 62,621 成员 307,251 内容