Delete specific lines from text in Java

Ads
 

Delete specific lines from text in Java

Hi,

beginning in java, i'm trying to delete specific line from a text file. Ex: i want to delete data between line 10 and 20 and between line 20 and 30.

or i want to specify line number that i want to delete.

how could it be possible with java. thanks a lot

View Answers

November 30, 2011 at 1:10 PM

This may not be the optimized one, but this works.

  1. Open the old file for reading
  2. Open a new (temporary) file for writing
  3. Iterate over the lines in the old file (probably using a BufferedReader)

     1.For each line, check if it matches what you are supposed to remove 
     2.If it matches, do nothing
     3.If it doesn't match, write it to the temporary file
    
  4. When done, close both files
  5. Delete the old file
  6. Rename the temporary file to the name of the original file

Ads









Related Tutorials/Questions & Answers:
Delete specific lines from text in Java
Delete specific lines from text in Java  Hi, beginning in java, i'm trying to delete specific line from a text file. Ex: i want to delete data... number that i want to delete. how could it be possible with java. thanks a lot
How to extract the entire line with specific data from a Text in java?
How to extract the entire line with specific data from a Text in java?  Hi, i would like to extract data from text like the following one: [CODE... my prog? i know how to read specific line from text with the lineNumber
Advertisements
Read Lines from text file
Read Lines from text file  Here's a brief desc of what my Java code does .. I'm using BufferedReader to read lines from a text files and split each... read from the text file and displays the output as desired. Unable to read the rest
Read specific column data from text file in java
Read specific column data from text file in java  My question is if my text file contain 15 columns and i want read specific column data from that text file then what code i should do
Java Read Lines from Text File and Output in Reverse order to a Different Text File
Java Read Lines from Text File and Output in Reverse order to a Different Text... to another text file. When that is done the output values of that file need... by line and output in reverse order to the text area field. Can someone help
To dump 10lakh lines from a text file to a mysql table
To dump 10lakh lines from a text file to a mysql table  I need to dump a text file datas -100000 lines into a mysql table.i'm able to dump only 152000 lines only
Java read lines from file
Java read lines from file  Any code example related to Java read lines from file? In my project there is requirement of reading the file line by line... of reading file line by line in Java. Can any one share me the code for reading
delete lines in the file
delete lines in the file   I am using a file as Input in program...The file contains round about 1,00,000 lines .My program after each iteration takes a data from that file at process it. My Question is when a particular data
delete lines in the file
delete lines in the file   I am using a file as Input in program...The file contains round about 1,00,000 lines .My program after each iteration takes a data from that file at process it. My Question is when a particular data
delete lines in the file
delete lines in the file   I am using a file as Input in program...The file contains round about 1,00,000 lines .My program after each iteration takes a data from that file at process it. My Question is when a particular data
ModuleNotFoundError: No module named 'extract-specific-lines'
ModuleNotFoundError: No module named 'extract-specific-lines'  Hi...: No module named 'extract-specific-lines' How to remove the ModuleNotFoundError: No module named 'extract-specific-lines' error? Thanks   Hi
ModuleNotFoundError: No module named 'extract-specific-lines'
ModuleNotFoundError: No module named 'extract-specific-lines'  Hi...: No module named 'extract-specific-lines' How to remove the ModuleNotFoundError: No module named 'extract-specific-lines' error? Thanks   Hi
delete a node from singly linked list in java
delete a node from singly linked list in java  Write a program(in java), if given a pointer to a node (not the tail node) in a singly linked list, delete that node from the linked list.   could you tell your question
Search and return lines of text file
Search and return lines of text file  I wrote the following code, that found lines of txt file with contains certain word, but it returns only the first or the last line! (However, the System.out.println show me all the lines
Count characters from text file in Java
is a java code that count the occurrence of each character from text file. import...Count characters from text file in Java  At the "Count chracters fro mtext file in Java". I tried to run the code, but the error at the line have
delete an item from database
delete an item from database  how to delete an item from the database using jsp
delete app from iphone
delete app from iphone  How to delete an existing app from itunes
Delete a file from FTP Server
In this section you will learn how to delete file from FTP server using java
Delete points from database
Delete points from database  I have a polygon on a web page...: mysqlquery("DELETE FROM polygon WHERE ID=".$GET['ID']."") or die(showsqlerrors(mysql_error())); I also used: mysqlquery("DELETE FROM points WHERE ID=".$GET['ID
java write to a specific line
java write to a specific line In this tutorial, you will learn how to write at specific line in text file. Java provides java.io package to perform file operations. Here, we are going to write the text to the specific line of the text
JDBC Delete Row In Table Example
specific row from the table use mysql JDBC driver.This tutorial defined how one or more specific  row delete from table that follow any given condition...  Mysql query  "DELETE FROM user where user_id=1 "  
Retrieving specific data from excel
Retrieving specific data from excel  Hello everyone, i have written a simple code to retrieve data from excel sheet and working fine, the excel file... first sheet from the workbook HSSFSheet sheet = workbook.getSheetAt(0
delete row from a table in hibernate
delete row from a table in hibernate  is there method to delete row in a table using hibernate like save(-) to insert row
How to delete data from MySQL?
How to delete data from MySQL?  Hi, How I can conditionally delete... to conditionally delete the data from MySQL database table. Here is some query examples: delete from email where email_count=400; delete from email where
Java 11 String lines function example to split it in lines
Java 11 String lines function - How to split String into lines in Java 11... is introduced in Java 11 which is used to split lines in Java. Java 11 introduced... lines(), which returns a stream of the lines from the string object. This method
Java 11 String lines function example to split it in lines
Java 11 String lines function - How to split String into lines in Java 11... is introduced in Java 11 which is used to split lines in Java. Java 11 introduced... lines(), which returns a stream of the lines from the string object. This method
delete row from a table using hibernate
delete row from a table using hibernate  //code in java file String hql="delete from CONTACT c where ID=6"; Query query=session.createQuery... [delete from CONTACT] int i=query.executeUpdate
How to delete line starting with a letter in java Regex?
How to delete line starting with a letter in java Regex?  Hi, i 'm trying to code with regex and java. i want to know how it's possible to delete lines which starts with a letter like G or g in a text. i tried
BufferedReader in Java
BufferedReader in Java BufferedReader in Java is used to to read characters, arrays, lines and File Line by line. It also reads text from a character.... BufferedReader class in Java is also used to read the data in buffer. Here we
Delete a row from database by id
Delete a row from database by id  I m creating a small application...) for "DELETE" AND "UPDATE". On clicking delete which is hyper link that particular row.... So anyone will tell me how to give hyper link to Delete and Update and delete
How to delete file in Java code?
How to delete file in Java code?  Hi, From my Java program I have to delete a file. How to delete file in Java code? Thanks   Hi, Java API provides methods for deleting a file in Java. You can use the delete

Ads