Hey guys, I've been looking at this for hours and can not determine what or why some of the code is holding a lock on the file. The code's intention is to go through a file and do a regex on the file replacing certain text.
The problem is, I create a temp file and put my results in it. Once it's finished I try to delete the main file, say MainFile.txt, so that I can rename the tmp file to MainFile.txt
Here is the code:
I've tried closing the FileInputStream and the FileChannel and even after closing them the file will still not DELETE. I'm kind of lost as to why the file won't be deleted.Code:private void convertXMLFile(File list) throws IOException {
Scanner s = null;
String current = null;
try {
s = new Scanner(new BufferedReader(new FileReader(list)));
File newOut = new File(list+".tmp");
BufferedWriter output = new BufferedWriter(new FileWriter(newOut));
FileInputStream fis = new FileInputStream(list);
FileChannel fc = fis.getChannel();
// Create a read-only CharBuffer on the file
ByteBuffer bbuf = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int)fc.size());
CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
// Create matcher on file
Pattern pattern = Pattern.compile("<sometext2>");
Pattern pattern2 = Pattern.compile("<sometext>");
Matcher matcher = pattern.matcher(cbuf);
Matcher matcher2 = pattern2.matcher(cbuf);
// Find all matches
if (matcher.find() || matcher2.find()){
// Get the matching string
System.out.println("Found: " + list.getName());
String lawl = matcher.replaceAll("\n");
s.useDelimiter("\\n");
while( s.hasNext() ){
current = s.next();
current = current.replaceAll("<sometxt2>", "\n");
current = current.replaceAll("<sometext>"," ");
output.append(current+"\n");
}
output.close();
s.close();
String tmp = list.getPath();
list.delete();
newOut.renameTo(new File(tmp));
cmp++;
}
} catch (FileNotFoundException e) {
err++;
System.out.println("File Not Found");
} catch (IOException e) {
err++;
}
}
Thanks for any help.

