Solved

How to Delete line in text file

Posted on 2001-09-03
5
782 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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

630 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