Solved

How to Delete line in text file

Posted on 2001-09-03
5
755 Views
Last Modified: 2007-12-19
I m storing ph.nos in text file line by line. I want to delete one phone no from the file.
I m using FileWriter and BufferedWriter.

plz, write any way to delete line.
0
Comment
Question by:macha060500
5 Comments
 
LVL 2

Expert Comment

by:sateesh_babu
ID: 6449920
Procedure is
Open a new file, and the current file.

Scan the current file line by line,
Loop
  if ( currentLine == ReqPhoneNumber)
  {
      Do not copy the line to the new file
  }
  else
  {
      Copy the line to the new file.
  }
End loop;

delete the old file.
Rename the new file to the old file name.
Close all the file handles.

Hope this helps!!!
Babu
0
 

Expert Comment

by:psvinayram
ID: 6450431
Also synchronize the method if more than one program or user is accessing the same file
0
 
LVL 2

Accepted Solution

by:
vasan_sr earned 10 total points
ID: 6450946
hi
 i hope this code will sort u r problem...
goodluck

import java.io.*;
import java.lang.*;

public class Test1 {
String thisLine;
int line;

     public void readFile(){
          try {
     PrintWriter pw=new PrintWriter(new FileOutputStream("output.txt"));
     StringBuffer sb=new StringBuffer("");
       FileInputStream fin =  new FileInputStream("input.txt");
        BufferedReader myInput = new BufferedReader(new InputStreamReader(fin));
             int cnt=1;
     while ((thisLine = myInput.readLine()) != null) {  
                if(!thisLine.equals(yourLine)){
       char[] c=thisLine.toCharArray();    
           for(int i=0;i<c.length;i++){
               sb.append(c[i]);
           }
          }
          }

       pw.println(sb.toString());
     pw.close();
       }catch (Exception e) {
                    e.printStackTrace();
               }
     }    
   

     public static void main(String args[])
     {
           Test1 t=new Test1();
           t.readFile();

          }
}
0
 
LVL 5

Expert Comment

by:vemul
ID: 7773189
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to vasan_sr

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Expert Comment

by:SpideyMod
ID: 7861438
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question