我很难删除文本文件中的一行行,我使用了这段代码,但我最终删除了所有行,需要一些帮助。
try //vacation leave/
{
File inputFile = new File("Adlawan" + code1);
File tempFile = new File("AdalwanTempFile");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
String lineToRemove = "AdlawanJan2012";
String currentLine;
while((currentLine = reader.readLine()) != null)
{
//trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.equals(lineToRemove))
{
writer.write(currentLine);
}
}
writer.close();
reader.close();
if(!inputFile.delete())
{
JOptionPane.showMessageDialog(null, "Could not rename file");
return;
}
if(!tempFile.renameTo(inputFile))
JOptionPane.showMessageDialog(null, "Could not rename file");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, " ");
}
文件中的数据如下所示:
AdlawanJan2012 假期假期积分 -0.6875
AdlawanFeb2012 假期假期积分 -0.6875
AdlawanMar2012 假期假期积分 -0.6875
修剪线条后的理想结果:
AdlawanFeb2012 假期休假积分 -0.6875
AdlawanMar2012 假期休假积分 -0.6875
谢谢...