Search code examples
javareplacewhile-loopbufferedreaderstringbuffer

Java compare strings from two places and exclude any matches


I'm trying to end up with a results.txt minus any matching items, having successfully compared some string inputs against another .txt file. Been staring at this code for way too long and I can't figure out why it isn't working. New to coding so would appreciate it if I could be steered in the right direction! Maybe I need a different approach? Apologies in advance for any loud tutting noises you may make. Using Java8.

//Sending a String[] into 'searchFile', contains around 8 small strings.
//Example of input: String[]{"name1","name2","name 3", "name 4.zip"}
                              ^ This is my exclusions list.

    public static void searchFile(String[] arr, String separator)
    {
        StringBuilder b = new StringBuilder();
        for(int i = 0; i < arr.length; i++)
        {
            if(i != 0) b.append(separator);
            b.append(arr[i]);
            String findME = arr[i];
            searchInfo(MyApp.getOptionsDir()+File.separator+"file-to-search.txt",findME);
        }
    }

^This works fine. I'm then sending the results to 'searchInfo' and trying to match and remove any duplicate (complete, not part) strings. This is where I am currently failing. Code runs but doesn't produce my desired output. It often finds part strings rather than complete ones. I think the 'results.txt' file is being overwritten each time...but I'm not sure tbh!

file-to-search.txt contains: "name2","name.zip","name 3.zip","name 4.zip" (text file is just a single line)

public static String searchInfo(String fileName, String findME)
{
    StringBuffer sb = new StringBuffer();
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));

        String line = null;
        while((line = br.readLine()) != null)
        {
            if(line.startsWith("\""+findME+"\""))
                 {
                    sb.append(line);
                        //tried various replace options with no joy
                        line = line.replaceFirst(findME+"?,", "");

                        //then goes off with results to create a txt file
                        FileHandling.createFile("results.txt",line);
                }
        }
    } catch (Exception e) {
        e.printStackTrace();        
    }
    return sb.toString();
}

What i'm trying to end up with is a result file MINUS any matching complete strings (not part strings):

e.g. results.txt to end up with: "name.zip","name 3.zip"


Solution

  • ok with the information I have. What you can do is this

    List<String> result = new ArrayList<>();
    String content = FileUtils.readFileToString(file, "UTF-8");
    for (String s : content.split(", ")) {
        if (!s.equals(findME)) { // assuming both have string quotes added already
            result.add(s);
        }
    }
    FileUtils.write(newFile, String.join(", ", result), "UTF-8");
    

    using apache commons file utils for ease. You may add or remove spaces after comma as per your need.