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?

4

4 回答 4

3

如果我正确理解了这个问题,这就是您需要的:

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();
}

注意break;. 这将停止 while 循环,因为您已经找到了您要查找的内容的答案。

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

但是您想知道的是是否根本没有field出现在文件中,而不是在每一行中。

于 2013-06-19T14:25:10.567 回答
1

您应该使用 Commons IO。

请参阅此工作示例

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);
    }
}
于 2013-06-19T14:29:07.433 回答
1

你应该这样做:

    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();
    }
于 2013-06-19T14:25:36.720 回答
0

由于您要求使用 Java,因此没有答案,只是为了说明 Unix shell 工具的强大功能:

v=phpcookbook.pdf
grep $v in.txt
[[ $? -eq 1 ]] && echo $v >> in.txt
于 2013-06-19T14:32:31.123 回答