1

Possible Duplicate:
Java - Find a line in a file and remove

I am trying to remove a complete line from a text file, and have managed to remove the line if there is only one single unbroken line of text without spaces. If i have a space delimiter between strings it fails to remove anything. Code as follows:

import java.io.*;
import java.util.Scanner;


public class removebooks {
     // construct temporary file
    public static void main(String[]args)throws IOException {
    String title;

     Scanner titlerem= new Scanner (System.in);
     System.out.println("Enter Title to remove from file");
     title = titlerem.next ();

     // construct temporary file
     File inputFile = new File("books.txt");
     File tempFile = new File(inputFile + "temp.txt");

     BufferedReader br = new BufferedReader (new FileReader("books.txt"));
     PrintWriter Pwr = new PrintWriter(new FileWriter (tempFile));
     String line = null;

     //read from original, write to temporary and trim space, while title not found
     while((line = br.readLine()) !=null) {
         if(line.trim().equals(title)){
             continue;          }
         else{
             Pwr.println(line);
             Pwr.flush();

         }
     }
     // close readers and writers
     br.close();
     Pwr.close();
     titlerem.close();

     // delete book file before renaming temp
     inputFile.delete();

     // rename temp file back to books.txt
     if(tempFile.renameTo(inputFile)){
            System.out.println("Update succesful");
        }else{
            System.out.println("Update failed");
        }
    }
}

the text file is called books.txt and contents simply should look like:

bookone author1 subject1
booktwo author2 subject2
bookthree author3 subject3
bookfour author4 subject4

thank you any help would be appreciated

Wojciech Wirzbicki
  • 3,887
  • 6
  • 36
  • 59
Matt Brookes
  • 29
  • 1
  • 2

3 Answers3

3

Why don't you use

if(line.trim().startsWith(title))

instead of

if(line.trim().equals(title))

because equals() is only true if both strings are equal, and startsWith() is true if line.trim() starts with title ;)

jlordo
  • 37,490
  • 6
  • 58
  • 83
  • i did search and find that sample code Woot4Moo but that is a single string in a line which i have achieved. The spaces between strings were causing me the problem. – Matt Brookes Jan 10 '13 at 20:52
1

As you are reading the file line by line. You can make use of following

  if(line.contains(title)){
     // do something
  }

In this case you will not be limited by title only.

String API

Smit
  • 4,685
  • 1
  • 24
  • 28
  • contains solved the problem. All the comments gave me a different direction to look in its difficult to get your head around as a learner. smit, JoshDm,jlordo thanks for your help :) – Matt Brookes Jan 10 '13 at 21:16
  • 1
    @MattBrookes I am glad it helped. jlordo suggested correctly by your implementation. However if you caught in any problem like this you can definitely look at ``Java API DOCs`. I included in my answer. – Smit Jan 10 '13 at 22:32
  • Based on which answer you ended up using, you should accept one so this question can be closed out. – JoshDM Jan 11 '13 at 17:05
0

br.readLine() sets the value of variable line to "bookone author1 subject1".

Scanner.next() delimits by whitespace. You need to consolidate all your calls to Scanner.next() to a single String before checking against the lines in the file, if that is your intent.

In your case, if you typed "bookone author1 subject1", value of variable title would be "bookone" after your call to Scanner.next().

JoshDM
  • 4,939
  • 7
  • 43
  • 72