JavaでINIファイルを削除するサンプルソース

コード下記
public static void DeleteINI(String file, String sect, String key, String value) {
BufferedReader input = null;
PrintWriter output = null;
try {
FileOutputStream foslock = new FileOutputStream(file + “.lock"); FileLock filelock = null;
try {
filelock = foslock.getChannel().lock();
} catch (IOException e) {
}
String file_cont = “";
boolean replaced = false;
File fh = new File(file);
if (fh.exists()) {
input = new BufferedReader(new FileReader(file));
String my_line;
Pattern p = Pattern.compile(“^\\[" + sect + “\\]" + key + “=(.*)$");
while ((my_line = input.readLine()) != null) {
Matcher m = p.matcher(my_line);
if (m.find()) {
// DELETE
} else if (my_line.equals(“%>")) {
// IGNORE
} else {
file_cont += my_line + “\r\n";
}
}
input.close();
} else {
file_cont += “<%" + “\r\n";
}
file_cont += “%>";
output = new PrintWriter(new FileOutputStream(file));
if (output != null) {
output.print(file_cont);
output.close();
}
if (filelock != null) filelock.release();
foslock.close();
File fhlock = new File(file + “.lock");
if (fhlock.exists()) fhlock.delete();
} catch (FileNotFoundException e) {
if (input != null) try { input.close(); } catch (IOException ee) { ; }
if (output != null) try { output.close(); } catch (Exception ee) { ; }
} catch (IOException e) {
if (input != null) try { input.close(); } catch (IOException ee) { ; }
if (output != null) try { output.close(); } catch (Exception ee) { ; }
}
}

Java

Posted by arkgame