Compare a text file line by line with a dynamic variable

 ̄綄美尐妖づ 提交于 2019-12-13 08:11:01

问题


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?


回答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);
    }
}



回答2:


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:


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:


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


来源:https://stackoverflow.com/questions/17193622/compare-a-text-file-line-by-line-with-a-dynamic-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!