1

I have a script which visits links from a text file. I am trying to delete the string if value returned is null

Example:

 1. some link (returned value 'hi')
 2. some link (returned null value)     //DELETE STRING FROM FILE BECAUSE NULL VALUE RETURNED
 3. some link (returned value 'hello')

Some code:

while ((input = in.readLine()) != null) {
                    System.out.println(input);

            if ((input = in.readLine())=="0"){

            System.out.println("1 String deleted from file because null value returned ");                  
    }

I'm aware that I'm checking for String "0" instead of an integer 0 because the server stores it as a string i suppose.

Sajidkhan
  • 11
  • 7
  • 2
    First of all, [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jason C Nov 03 '14 at 20:05
  • Secondly, you'll either want to re-write just the lines you want to keep to a temporary file as you go then copy it back over the original, or read all lines into some array first, process them, remove whatever lines in memory, then write the full modified array back out all at once. You can't just delete an arbitrary line from a text file directly, unfortunately. – Jason C Nov 03 '14 at 20:06
  • And finally, note that you're reading two lines per loop iteration. – Jason C Nov 03 '14 at 20:10
  • The file grows very large in a matter of minutes (and hence the question) because of another script adding links to it continously. I had come across the temp file solution but that would still hurt the memory right? – Sajidkhan Nov 03 '14 at 20:11
  • 1
    @Sajidkhan If you've got another file writing to it constantly, don't you think you're going to have trouble if you use this code to *also* write constantly to it? – furkle Nov 03 '14 at 20:13
  • You cannot remove arbitrary lines from a text file directly. Period. You will have to find another way. Why don't you look into e.g. an SQLite database? SQLite doesn't require any database server, just a data file; you can easily have one app adding records while the other processes and removes, and you may learn a few new useful skills in the process. A flat text file is not generally the best for storing processing queues like that; and, ignoring memory (which may not even be a problem) you *will* run into concurrency issues even if you get the line removal down. – Jason C Nov 03 '14 at 20:13
  • Another option is to look into some method of IPC, perhaps even something as basic as local network sockets, so that your script can send the links *to* your program. Or, perhaps invoke the script from your program and read its output directly, as it is running. You will want to find another way to do this, though. – Jason C Nov 03 '14 at 20:14
  • I have a PHP script doing the same thing but I would like to use Java for this. Can you provide me a close enough reference for using Java-SQLite for similar operations? Though I'm also researching it – Sajidkhan Nov 03 '14 at 20:31

1 Answers1

1

I think, rather than trying to remove to the file mid-read (and I don't even really know how you'd do that, and if you could it'd be a horrible idea) you might have an easier time of this by just reading the entire file in and storing each value in an index of an ArrayList<string>:

ArrayList<string> lines = new ArrayList<string>();
while ((input = in.readLine()) != null) {
    lines.add(input);
}

Then write the file again after you've finished reading it, skipping any index of lines that's equal to "0":

for (String line : lines)
 {
     // skip "0"
     if (line.equals("0")) { 
         continue;
      }

      // write to file if not      
      writer.write(line);
      writer.newLine();
 } 

Note that == compares reference equality in Java, and .equals compares value equality, so for almost all cases you want to use .equals.

Granted, if as your comment states above, you have another file constantly writing to this one, you're better off looking for an entirely new idea. For that matter, if you've got a script writing these, why not change the script so that it just doesn't write lines for null values in the first place? Unless you have literally no way at all of changing the script, spinning another one up to constantly rewrite parts of its work (on the same constantly-accessed file!) is going to be a. ineffective and b. extremely problematic.

furkle
  • 5,019
  • 1
  • 15
  • 24
  • The links are generated and not scraped so I must check them to retrieve values. Reading the file is not the problem and though your solution is helpful, I really am looking for ways to delete a string after it's been retrieved because of memory usage. – Sajidkhan Nov 03 '14 at 20:39
  • @Sajidkhan Sorry, what exactly do you mean by "delete a string after it's been retrieved"? – furkle Nov 03 '14 at 20:40
  • A script generates links which are stored in a text file and then checked by another script. I would like to delete string(s) from the file whose values are returned to be zero. – Sajidkhan Nov 03 '14 at 20:41
  • @Sajidkhan That's a pretty strong indicator that you're going about this the wrong way. Either you should find a way to stop the script from writing null values in the first place, as I said, or use some other storage solution as Jason suggested. (Even if you use SQL or SQLite, you should find a way to prevent null writes.) – furkle Nov 03 '14 at 20:44
  • I am looking into it right now, but there's no way I can prevent the script from writing null values beforehand. How would you know if a value is null without checking it? – Sajidkhan Nov 03 '14 at 20:46
  • @Sajidkhan What does this mean? Presumably the script has some ability to reason about the information it's taking in before writing it. Do you mean a logger, not a script? – furkle Nov 03 '14 at 21:28