Solved

How to Delete line in text file

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Jpanels and Jframe 8 32
simple java question 3 55
servlet filter example 37 59
Desingning Refactoring existing code 2 24
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

786 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