Compare a text file line by line with a dynamic va

2019-08-19 06:50发布

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?

4条回答
Viruses.
2楼-- · 2019-08-19 06:58

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.

查看更多
聊天终结者
3楼-- · 2019-08-19 07:10

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();
    }
查看更多
男人必须洒脱
4楼-- · 2019-08-19 07:13

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);
    }
}
查看更多
Evening l夕情丶
5楼-- · 2019-08-19 07:14

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
查看更多
登录 后发表回答