0

I am looking for a solution to the following problem. I am creating a .txt file and then adding values into it. I am running a loop and don't want the the same values repeated in the txt file. So basicly I need a IF STATEMENT that reads the .txt file and checks for the value and then for it to add the value to the .txt file if it does not have it already in there. A example of some of myt code is below.

String Friends = Dave.get(friends);
String file = "C:/Users/george.bell/Desktop/Failed2/failedraces.txt";
FileWriter fw = new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);

If statement goes here to see if Dave's friends are already in the .txt file

bw.write(Friends);
bw.newLine();
bw.close();

so basicly i want to know how to check a .txt file for values.

the format of the .txt file is the following:

Jake
Mike
Fred
4
  • 1
    which is the format of friends in the txt file? May 22, 2015 at 10:17
  • @jordiCastilla it has the format like: jake (newline) fred (newline)...ect. comments dont let you make a new line but you get the idea
    – Grbe1l
    May 22, 2015 at 10:19
  • Have you looked at FileReader and BufferedReader? Note that you cannot open a FileReader for a file that you are currently writing to, so you should first read the file, close it, and then reopen it for writing. May 22, 2015 at 10:20
  • @alexanderWeinert i have looked into them but not sure how to use them in this instance
    – Grbe1l
    May 22, 2015 at 10:22

3 Answers 3

3

You have to check which friends are NOT on the text file, so catch them and put into a list:

String file = "C:/Users/george.bell/Desktop/Failed2/failedraces.txt";
BufferedReader in = new BufferedReader(new FileReader(file));
String str;

List<String> friendsInFile = new ArrayList<String>();
while((str = in.readLine()) != null){
    friendsInFile.add(str);
}

You can achieve same using Apache Commons IO

String file = "C:/Users/george.bell/Desktop/Failed2/failedraces.txt";
List<String> friendsInFile = IOUtils.readLines(new FileInputStream(file));

Then you can make your if with List.contains() method, iterate over Dave's friends List

for (String friend : dave.getFriends()) {
    if (!friendsInFile.contains(friend)) {
        // add the friend to the file
    }
}
1
  • great to hear that @Grbe1l ;) May 22, 2015 at 10:46
1

you can use split() or you can try to read whole file as a string and check weather a name is present or not in the string (you can also try to loop over names and check them one by one). If a given name does not exist you can try to write it into a file.

just a sample algo

  1. read complete file as a String.
  2. check the string if it consists of value.
  3. if the value exists do nothing,
  4. if value does not exists try to append the file with that name.

Hope this helps : )

1

You can use a Set that is a container with no duplicates. Everytime you read a name introduce it to the set. Later write the entire Set to your text file. No duplicates will appear.

Hope this helps you! :)

2
  • "Everytime you read a name check if it's already in the Set (contains(Object o)) and then if returns false, introduce it to the set." ... so why should he check first, if he wants that name in the Set? He can simply add it.
    – Tom
    May 22, 2015 at 10:40
  • 1
    Yes it's correct sorry, only add the read names to set and later write the entire set in the text file. I will edit answer.
    – piraces
    May 22, 2015 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.