Solved

How to Delete line in text file

Posted on 2001-09-03
5
717 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now