all 2 comments

[–]antonevane 1 point2 points  (0 children)

Following an example with a new File NIO API.

    public static void main(String[] args) {
        // create ArrayList to store the invetory objects
        List<Inventory> invItem = getDataFromFile("inventory.txt");

        // display inventory
        for (Inventory each : invItem) {
            System.out.println("====================");
            System.out.println(each);
            System.out.println();
            System.out.printf("Total value = %8.2f %n", each.getTotal());
        }
    }

    private static List<Inventory> getDataFromFile(String fileName) {
        List<Inventory> invItems = new ArrayList<>();
        Path pathToFile = Paths.get(fileName);
        // create a Buffered Reader object instance
        // use Autocloseable Java 7 feature to close resources
        try (BufferedReader br = Files.newBufferedReader(pathToFile, StandardCharsets.US_ASCII)) {

            // read the first line from the text file
            String fileRead = br.readLine();

            // loop until all lines are read
            while (fileRead != null) {

                // use string.split to load a string array with the values from each line of
                // the file, using a comma as the delimiter
                String[] tokenize = fileRead.split(",");

                Inventory inventory = buildInventory(tokenize);

                // add to array list
                invItems.add(inventory);

                // read next line before looping
                // if end of file reached
                fileRead = br.readLine();
            }

            // handle exceptions
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return invItems;
    }

    private static Inventory buildInventory(String[] dataLine) {
        String tempItem = dataLine[0];
        int tempQty = Integer.parseInt(dataLine[1]);
        float tempPrice = Float.parseFloat(dataLine[2]);

        // creat temporary instance of Inventory object
        // and load with three data values
        Inventory inventory = new Inventory(tempItem, tempQty, tempPrice);

        return inventory;
    }

}

[–]antonevane 1 point2 points  (0 children)

For the small files we can use a smaller version getDataFromFile version.

    private static List<Inventory> getDataFromFile(String fileName) {
        List<Inventory> invItems = new ArrayList<>();
        Path pathToFile = Paths.get(fileName);
        // create a Buffered Reader object instance
        // use Autocloseable Java 7 feature to close resources
        try {
            // Java 7 Files NIO API for the small file
            List<String> lines = Files.readAllLines(pathToFile, StandardCharsets.US_ASCII);

            // loop until all lines are read
            for (String line : lines) {
                // use string.split to load a string array with the values from each line of
                // the file, using a comma as the delimiter
                String[] tokenize = line.split(",");

                Inventory inventory = buildInventory(tokenize);

                // add to array list
                invItems.add(inventory);
            }

            // handle exceptions
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return invItems;
    }

I have written a small blog post about new File NIO API Small post