Q:

Add a string to a file ONLY if it doesn't exist

I'm teaching myself Java and came across a problem which I have no idea how to solve.
I want to basically check two things:
1. If a file doesn't exist - create it! if it does, do nothing.
2. If the file contains a given String, do nothing, if it does not contain it - add it! (don't overwrite it)
2nd one is more important but I couldn't figure out the first one either.
Tried looking online on how to make sure a file exists, or how to only add String to a file if it doesn't exist, but it just doesn't work for some reason.
main{        
String s;
FileWriter fw = new FileWriter("s.txt", true);
            File file = new File("s.txt");
            doesStringExist(s,fw);   
} 

public void doesStringExist(String s, FileWriter fw) throws IOException {
        String scan;
        BufferedReader bReader = new BufferedReader(new FileReader(String.valueOf(fw)));
        while ((scan = bReader.readLine()) != null) {
            if (scan.length() == 0) {
                continue;
            }
            if(scan.contains(s) {
                System.out.println(s + " already exists in S.txt");
            }else{
                fw.write(s);
            }
        }
    }

// I made a different method for checking if it exists or not because i just like it like that being more organized

Currently I expect the code to only check if a string exists, if it does, do nothing (send exists message), if it doesn't exist, add it to the file.
also I want to make it so it checks if the file exists.

A:

Strictly said I might not use exists() after all, just go with the exception-path:
File file = new File("s.txt");  // this is a file handle, s.txt may or may not exist
boolean found=false;  // flag for target txt being present
try(BufferedReader br=new BufferedReader(new FileReader(file))){
  String line;
  while((line=br.readLine())!=null)  // classic way of reading a file line-by-line
    if(line.equals("something")){
      found=true;
      break;  // if the text is present, we do not have to read the rest after all
    }
} catch(FileNotFoundException fnfe){}

if(!found){  // if the text is not found, it has to be written
  try(PrintWriter pw=new PrintWriter(new FileWriter(file,true))){  // it works with
                                                                   // non-existing files too
    bw.println("something");
  }
}

