Remove specific section from text file using java
9:04 PM Posted by Preetish Panda
In this post I'll share a code snippet that will remove specific section from the text file.Suppose your file has following structure:
Something
blah blah blah
START
lines...lines..
more..lines..
Our code will remove all the text present between and including START & END.
Here is the method:
Something
blah blah blah
START
lines...lines..
more..lines..
END
again some textOur code will remove all the text present between and including START & END.
Here is the method:
public void removeLineFromFile(String file, String start, String end) {
try {
File inFile = new File(file);
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
Boolean flag=true;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (line.trim().equals(start)) {
flag=false;
}
if(line.trim().equals(end)){
flag=true;
}
if (flag && !(line.trim().equals(end))){
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
Happy coding !! :)try {
File inFile = new File(file);
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
Boolean flag=true;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (line.trim().equals(start)) {
flag=false;
}
if(line.trim().equals(end)){
flag=true;
}
if (flag && !(line.trim().equals(end))){
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
Click here to Subscribe news feed from "TechFuzon", so that you do not miss out anything that can be valuable to you !!

















0 comments:
Post a Comment