Solved

delete a line from a file

Posted on 2000-05-11
4
231 Views
Last Modified: 2010-04-01
I have a file which is something like this:

aaaa:zzz
bb:yy
ccccccc:xx

Now I search through the file for a particular string, say "bb". Once I get the match in the file I want to delete that line "bb:yy". How do I do that?

This file will be accessed by scripts many times, could be at the same time.
I need to take care of that too?

-Sunita
0
Comment
Question by:ssunitas
[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
  • 2
4 Comments
 

Expert Comment

by:ashcrowd
ID: 2801406
Use an input and output buffer as you read each line in if it is ok append it to the output buffer otherwise read the next line.
0
 

Author Comment

by:ssunitas
ID: 2801780
How can I read the whole file in one shot into another file or into an array?
0
 

Accepted Solution

by:
java_world earned 10 total points
ID: 2804500
Hi Sunita,
What you do?
Search for your String and at the same time,
store the lines which doesn't have your String
and then at last i.e. at the end of line,
copy that Long String into your file Ok by recreating the same file.
Your doubt is that how can i have next lines in the String. It's easy yaar, just you Concat with
"\n" wherever you want to have a next line.
Satisfied.
Try it out, I am there
bye
-praveen from U.S.
0
 

Expert Comment

by:ashcrowd
ID: 2804543
I don't think you would want to read the whole file at once just a line at a time.  The fileBufferReader will do that for you.


try
  {
        aFile = new bufferedReader(new FileReader(fileName));
      while (aFile.readLine() != null)
      {
          //do your checks here if ok add line to output
      }

  }
catch (IOException exception)
      {
                        //io error here
                 }

            
0

Featured Post

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

707 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