0

I have a text file sample.txt which have following lines

sample1.txt
test.ppt
example.doc
content.pdf

I have a dynamic variable called field (example phpcookbook.pdf,sample1.txt) it should compare with each line in sample.txt file and if the text file does not contain the field it should append to sample.txt. I have tried the following code but it's not working:

File insert=new File(sample.txt);
BufferedReader br = new BufferedReader(new FileReader(insert));
String strLine;

while ((strLine = br.readLine()) != null) {
    if(!strLine.equals(field)) {
        FileWriter fw = new FileWriter(insert, true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(field);
        bw.close();
    }
}

I should get the following output

sample1.txt
test.ppt
example.doc
content.pdf
phpcookbook.pdf

How to compare a text file line by line with a dynamic variable?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2353439
  • 489
  • 2
  • 7
  • 18

4 Answers4

3

If I understand the question correctly, this is what you need:

File insert = new File("sample.txt");
BufferedReader br = new BufferedReader(new FileReader(insert));
String strLine;
Boolean hasLine = false;

while ((strLine = br.readLine()) != null) {
    if(strLine.equals(field)) {
        hasLine = true;
        break;
    }
}

br.close();

if (!hasLine) {
    FileWriter fw = new FileWriter(insert, true);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.append(field + "\n"); // assumes field does not already have a newline
    bw.flush();
    bw.close();
}

Notice the break;. This will discontinue the while loop, since you already have the answer to what you are looking for.

Your original code was doing:
for every line:
  Do I equal field?
    Yes: goto next line
    No: append field to file and goto next line

But what you WANTED to know was whether or not field appeared in the file at all, not in each line.

Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
  • thanks for the reply, it is not allowing duplicate values but it is not appending the different value other than the lines of files. – user2353439 Jun 19 '13 at 14:37
  • @user2353439 define "it". _My code_ is not allowing duplicates, but is also not appending `field`? Or are you talking about something else? – Russell Uhl Jun 19 '13 at 14:40
  • @user2353439: I've updated my code to include a newline character and flush statement. That may be causing you issues as well. – Russell Uhl Jun 19 '13 at 14:42
  • @RussellUhl Your code is working perfectly, i think he is not referring to the right sample.txt file which is getting updated. – ajay.patel Jun 19 '13 at 14:49
  • @zerocool: Ahhhh gotcha. That would do it. Thanks, Crash Override – Russell Uhl Jun 19 '13 at 14:56
1

You should do something like this:

    File insert = new File("sample.txt");
    boolean isStringPresent = false;
    BufferedReader br = new BufferedReader(new FileReader(insert));
    String strLine;
    while ((strLine = br.readLine()) != null) {
        if (strLine.equals(field)) {
            isStringPresent = true;
        } 

    }
    if(!isStringPresent) {
        FileWriter fw = new FileWriter(insert, true);
        BufferedWriter bw = new BufferedWriter(fw);
        bw.append(field);
        bw.close();
    }
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
1

You should use Commons IO.

See this working example

package training;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

public class TestFile {

    /**
     * @param args
     * @throws IOException
     */
    private static String field = "phpcookbook.pdf";
    private static String fileContent;

    public static void main(String[] args) throws IOException {
        boolean found = false;
        File file = new File("test.txt");
        List<String> lines = FileUtils.readLines(file);
        for (String line : lines) {
            if (line.equals(field)) {
                found = true;
                break;

            }
        }
        if (!found) {
            fileContent = FileUtils.readFileToString(file);
            fileContent += "\n" + field;
        }
        FileUtils.write(file, fileContent);
    }
}
Makky
  • 17,117
  • 17
  • 63
  • 86
0

No answer since you ask for Java, but just to illustrate the power of Unix shell tools:

v=phpcookbook.pdf
grep $v in.txt
[[ $? -eq 1 ]] && echo $v >> in.txt