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 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









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
Read Specific Line from file Using Java
Read Specific Line from file Using Java Here we are going to read a specific line from the text file. For this we have created a for loop to read lines 1 to 10 from the text file. If the loop reached fifth line, the br.readLine() method
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
How to extract a specific line from a text file? - IoC
How to extract a specific line from a text file?  Hi all, i'm trying to write a code that return a specific line from a text file. so my first..., it's working fine and it's from Roseindia tutorial. But now, i want to extract the line N
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 Specific Row from a Database Table
Delete a Specific Row from a Database Table   ... to delete a specific row then you use some java methods and APIs interfaces. Detail.... After establishing the connection we are going to delete a specific row from
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
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
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 data from database - SQL
delete data from database  HOw to delete specific data from table> I want to delete one record from database. Thnx  Hi friend, -------------------------------------------- Visit for more information
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
Reading text from image file - Java Beginners
Reading text from image file  How Read text from image file
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
How to get specific index value from ByteBuffer in java.
How to get specific index value from ByteBuffer in java.    ... will discuss how to get value of a given index from buffer. The ByteBuffer ... C:\Work\Bharat\load\getSpecified value>java GetIndexValue Value
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 "  
How to delete a character from a file in java - Java Beginners
How to delete a character from a file in java  I'm not gettting how to remove a character from a file in java....could any one help me out?? ... or not and give in details and visit to : http://www.roseindia.net/java
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 read text from - Java Beginners
How to read text from   How to retrieve text from the images... Does we have any function to get text over the images  Hi Friend, We are providing you a code that will set text over an image using javascript
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
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
Delete a file from FTP Server
In this section you will learn how to delete file from FTP server using java
Character from text file - Java Beginners
Character from text file  Write a program that reads characters from a text file. Your program will count how many time each character appear in the text. Assume that the letters are case-sensitive. Example output
Java file number of lines
Java file number of lines In this section, you will learn how to count the number of lines from the given file. Description of code: Java has provide... the lines from the file.ADS_TO_REPLACE_1 You can see in the given example, 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
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
Text From Your Clipboard?
Text From Your Clipboard?  Text From Your Clipboard
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
how to delete a jar from mobile
how to delete a jar from mobile  After auto updating a new version of jar from server. I want to delete the old jar from mobile and want to install the new jar which is present in server. Can u tell me the code how to do
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
Delete row and column from table through java code
Delete row and column from table through java code... will see how to delete row and column from given table through java code. Java code...;select min(ID) from stu_info"); rs.next(); // This sql query delete
How to remove specific element from Hashset.
How to remove specific element from Hashset. In this  exmple, you will see how to remove specific element from hashset. The HashSet class provides a method called remove. It removes a elements from set. Code: 

Ads