private void loadDBFile() { try { BufferedReader reader = new BufferedReader(new FileReader(new File("formula_database.dat"))); String line = reader.readLine(); while (line != null) { String[] itemAttrs = line.split("[|]"); InventoryItem item = new InventoryItem(); item.setName(itemAttrs.length > 1 ? itemAttrs[1] : ""); item.setBidFormula(itemAttrs.length > 2 ? itemAttrs[2] : ""); item.setSellFormula(itemAttrs.length > 3 ? itemAttrs[3] : ""); if (itemAttrs.length > 4) { item.setFavorite(true); if (modelMap.containsKey(itemAttrs[4])) { modelMap.get(itemAttrs[4]).addRow(item); } } if (modelMap.containsKey(itemAttrs[0])) { InventoryTableModel model = modelMap.get(itemAttrs[0]); // Identifier model.addRow(item); } line = reader.readLine(); } } catch (FileNotFoundException e) { System.out.println("Warning: No Inventory file found in working directory."); } catch (IOException e) { System.out.println("Error reading line from Inventory file."); } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Inventory file is malformed"); } }
public void updateDB() { FileWriter writer = null; try { writer = new FileWriter(new File("formula_database.dat")); StringBuffer buffer = new StringBuffer(); for (InventoryTableModel model : modelMap.values()) { ArrayList<InventoryItem> dataItems = model.getData(); if (model.getType().indexOf("FAVORITES") == -1) { for (InventoryItem item : dataItems) { String publicOrDealer = model .getType() .substring(model.getType().indexOf("_") + 1, model.getType().length()); buffer.append(model.getType()); buffer.append("|"); buffer.append(item.getName() == null ? "" : item.getName()); buffer.append("|"); buffer.append(item.getBidFormula() == null ? "" : item.getBidFormula()); buffer.append("|"); buffer.append(item.getSellFormula() == null ? "" : item.getSellFormula()); buffer.append("|"); if (item.isFavorite()) { buffer.append("FAVORITES_" + publicOrDealer); buffer.append("|"); } buffer.append("\n"); writer.write(buffer.toString()); buffer.setLength(0); } } } } catch (IOException ioe) { System.out.println("error reading input to file writer"); } finally { try { writer.close(); } catch (IOException e) { System.out.println("error closing buffered writer"); } } }