/** * Description of loadDate()) Constructs a person bean from each line of text from the input file * and adds it to an observable list. */ private void loadData() { try { // System.out.println(Paths.get("mydata.txt").toAbsolutePath()); // List<String> lines = Files.readAllLines(Paths.get("build/resource/mydata.txt"), // Charset.defaultCharset()); BufferedReader br = new BufferedReader(new FileReader("build/resource/mydata.txt")); String line; String data[] = new String[4]; while (true) { line = br.readLine(); if (line == null) { break; } else { data = line.split(","); System.out.println(data); PersonBean p = new PersonBean(data[0], data[1], data[2], new Integer(data[3])); personData.add(p); } } // *****End for br.close(); System.out.println("Loaded " + data.length + " rows of data."); } catch (Exception e) { System.out.println("There was a problem loading the file:" + e.getMessage()); e.printStackTrace(); } }
/** Writes the data to file. */ private void writeIt() { try { // FileWriter writer = new FileWriter("build/resource/mydata.txt"); BufferedWriter writer = new BufferedWriter(new FileWriter("build/resource/mydata.txt")); Iterator<PersonBean> it = personData.iterator(); while (it.hasNext()) { String x; PersonBean p = it.next(); writer.write( p.getFirstAndLastName() + "," + p.getEmail() + "," + p.getGender() + "," + p.getAge()); if (it.hasNext()) { writer.newLine(); } System.out.println("First and last: " + p.getFirstAndLastName()); } writer.close(); } catch (Exception e) { System.out.println("There was a problem saving the file:" + e.getMessage()); } }