Reputation: 489
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?
Upvotes: 0
Views: 4346
Reputation: 4531
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.
Upvotes: 3
Reputation:
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
Upvotes: 0
Reputation: 17461
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);
}
}
Upvotes: 1
Reputation: 68715
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();
}
Upvotes: 1