I have to add locks to my readFromFile() and writeToFile() methods for school. Please see the method code below and advise me where in the code I would place the locks? Would I use ReentrantLock or ReadWriteReentrantLock? We have to use locks.
Thank you.
Read method
public static void readFromFile(List<Person> people) { FileReader fr = new FileReader("names"); Scanner sc = new Scanner(fr); while (sc.hasNext()) { String name = sc.nextLine(); int age = Integer.parseInt(sc.nextLine()); people.add(new Person(name, age)); } sc.close(); } Write method
public static void writeToFile(List<Person> people) { File outputFile = new File (List<Person> people) PrintWriter pw = new PrintWriter(outputFile); for (Person p: people) { pw.println(p.name); } pw.close(); }