Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to Delete line in text file

Posted on 2001-09-03
5
Medium Priority
?
799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 40 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

670 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