Solved

Removing a line from file

Posted on 2004-10-04
11
210 Views
Last Modified: 2010-03-31
If I have a file named book.java

and it contains:

book1
book2
book3

now I want to remove book2 so that there's no blank line:

book1
book3

how can I do that? Thanks!
0
Comment
Question by:izomax
11 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12222731
you'll have to create a new file and write the lines you want to save to the new file and then once done replace the existing file with the new one

0
 

Author Comment

by:izomax
ID: 12222781
Does that mean I have to use RandomAccessFile?
0
 
LVL 92

Expert Comment

by:objects
ID: 12222835
No RAF won't help, it only allow you to overwrite data.
It doesn't let you remove data, or insert data.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12222851
No. You'll have to read in all the data, cut out what you don't want, and then output the whole string. Note, if you want to keep your line breaks, use BufferedReader.readLine() and append a "\n" to each line read in. To cut out the data:
int cutPoint = wholeString.indexOf(String stringToCut);
finalString = wholeString.substring(0, cutPoint) + wholeString.substring((cutPoint + 1), wholeString.length());

Then just output finalString to the file (make sure to overwrite).
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12222874
Errr... Oops. Don't declare the stringToCut in the indexOf...

This:
int cutPoint = wholeString.indexOf(String stringToCut);
should be this:
int cutPoint = wholeString.indexOf(stringToCut);

Oh, and add in after that line:
if (cutPoint == -1){//Error message followed by System.exit(0) or something as a value of -1 will screw up the substring
}

P.S. I assume that all the strings I used were declared earlier. If not, declare them, i.e. String finalString...
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12224690
use this code. As u've to replace "book2" value with "" empty. and also u've to remove leading new line "\n" char. U'll get ur desired result. Try this code.

    String thisText = "book1\nbook2\nbook3";
    String strRemove = "book2";
    String result = "";
    result = thisText.replaceAll("\n" + strRemove,"");
    System.out.println(thisText);
    System.out.println(result);


bye,
Naeem Shehzad Ghuman
0
 
LVL 3

Accepted Solution

by:
CI-Ia0s earned 500 total points
ID: 12233317
Gah, I just realized another small bug with my code; here's the correct version:

int cutPoint = wholeString.indexOf(stringToCut);
finalString = wholeString.substring(0, cutPoint) + wholeString.substring((cutPoint + cutPoint.length()), wholeString.length());

Note that since you append "\n" to your inputs (demoed below) you'll need to include "\n" at the end of stringToCut. Now, input:

String wholeString = "";
try{
BufferedReader streamIn = new BufferedReader(new FileReader("fileName.txt"));
String inString = "";
while (inString != null){
wholeString = wholeString + inString + "\n";
inString = streamIn.readLine();
}
catch (Exception e){
System.out.println("Bad file input.");
System.exit(1);
}
0
 
LVL 92

Expert Comment

by:objects
ID: 12233363
File f = new File("book.java");
File newf = new File("book.java.new");
BufferedReader in = new BufferedReader(new FileReader(f));
PrintWriter out = new PrintWriter(new FileWriter(newf));
String line = null;
while (null!=(line=in.readLine()))
{
   if (!line.equals("book2"))
   {
      out.println(line);
   }
}
in.close();
out.close();
f.delete();
newf.renameTo(f);
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12233392
Yea... That'd work too. ;)
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12233395
So you have your pick of 3 methods, izomax...
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12255555
Thank you. :)
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
splitOdd10 challenge 5 104
netstat -ano | find "8000" and taskkill /f /pid 2984 3 55
check java version using powershell 13 80
printf performancy 10 21
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

815 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

13 Experts available now in Live!

Get 1:1 Help Now