From 5a25feafef5dabc4930a1f4164f1a311ef11f12d Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Mon, 7 Sep 2015 16:34:48 -0600 Subject: [PATCH] Fixed path issue using nio Path --- patterns/doubledispatch/DoubleDispatch.java | 2 +- patterns/dynatrash/DynaTrash.java | 2 +- patterns/recycleap/RecycleAP.java | 2 +- patterns/recycleb/RecycleB.java | 2 +- patterns/trash/ParseTrash.java | 19 +++++++++++-------- patterns/trashvisitor/TrashVisitor.java | 2 +- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/patterns/doubledispatch/DoubleDispatch.java b/patterns/doubledispatch/DoubleDispatch.java index 808d62e6..8dfbabdf 100644 --- a/patterns/doubledispatch/DoubleDispatch.java +++ b/patterns/doubledispatch/DoubleDispatch.java @@ -56,7 +56,7 @@ public class DoubleDispatch { ArrayList bin = new ArrayList<>(); TrashBinSet bins = new TrashBinSet(); // ParseTrash still works, without changes: - ParseTrash.fillBin("DDTrash.dat", bin); + ParseTrash.fillBin("doubledispatch", "DDTrash.dat", bin); // Sort from the master bin into the // individually-typed bins: bins.sortIntoBins(bin); diff --git a/patterns/dynatrash/DynaTrash.java b/patterns/dynatrash/DynaTrash.java index 47885a26..6dd5cc9a 100644 --- a/patterns/dynatrash/DynaTrash.java +++ b/patterns/dynatrash/DynaTrash.java @@ -44,7 +44,7 @@ public class DynaTrash { @SuppressWarnings("unchecked") public static void main(String[] args) { TypeMap bin = new TypeMap<>(); - ParseTrash.fillBin("Trash.dat", + ParseTrash.fillBin("recycleap", "Trash.dat", new TypeMapAdapter(bin)); Iterator keys = bin.keys(); while(keys.hasNext()) diff --git a/patterns/recycleap/RecycleAP.java b/patterns/recycleap/RecycleAP.java index 40a667db..afcc82f3 100644 --- a/patterns/recycleap/RecycleAP.java +++ b/patterns/recycleap/RecycleAP.java @@ -9,7 +9,7 @@ public class RecycleAP { public static void main(String[] args) { ArrayList bin = new ArrayList<>(); // Fill up the Trash bin: - ParseTrash.fillBin("Trash.dat", bin); + ParseTrash.fillBin("recycleap", "Trash.dat", bin); List glassBin = new ArrayList<>(); List paperBin = new ArrayList<>(); List alBin = new ArrayList<>(); diff --git a/patterns/recycleb/RecycleB.java b/patterns/recycleb/RecycleB.java index cc4e7545..89b12f8a 100644 --- a/patterns/recycleb/RecycleB.java +++ b/patterns/recycleb/RecycleB.java @@ -41,7 +41,7 @@ public class RecycleB { static Tbin bin = new Tbin<>(Trash.class); public static void main(String[] args) { // Fill up the Trash bin: - ParseTrash.fillBin("Trash.dat", bin); + ParseTrash.fillBin("recycleap", "Trash.dat", bin); TbinList trashBins = new TbinList<>(); trashBins.add(new Tbin<>(Aluminum.class)); diff --git a/patterns/trash/ParseTrash.java b/patterns/trash/ParseTrash.java index 7e6dbae6..ed390b4d 100644 --- a/patterns/trash/ParseTrash.java +++ b/patterns/trash/ParseTrash.java @@ -5,22 +5,25 @@ package patterns.trash; import java.util.*; import java.io.*; +import java.nio.file.*; public class ParseTrash { public static void - fillBin(String filename, Fillable bin) { + fillBin(String dir, String fname, Fillable bin) { try { - try(BufferedReader data = new BufferedReader( - new FileReader(filename))) { + Path p = FileSystems.getDefault() + .getPath("..", dir, fname); + try(BufferedReader data = Files.newBufferedReader(p)){ String buf; while((buf = data.readLine())!= null) { if(buf.trim().length() == 0) continue; // Skip empty lines + if(buf.startsWith("//")) + continue; // Skip comments String type = buf.substring(0, - buf.indexOf(':')).trim(); + buf.indexOf(':')).trim(); double weight = Double.valueOf( - buf.substring(buf.indexOf(':') + 1) - .trim()); + buf.substring(buf.indexOf(':') + 1).trim()); bin.addTrash(Trash.factory( new Trash.Info(type, weight))); } @@ -34,7 +37,7 @@ public class ParseTrash { } // Special case to handle ArrayList: public static void - fillBin(String filename, ArrayList bin) { - fillBin(filename, new FillableList<>(bin)); + fillBin(String dir, String fname, ArrayList bin) { + fillBin(dir, fname, new FillableList<>(bin)); } } diff --git a/patterns/trashvisitor/TrashVisitor.java b/patterns/trashvisitor/TrashVisitor.java index 8653a362..d3b6cc7b 100644 --- a/patterns/trashvisitor/TrashVisitor.java +++ b/patterns/trashvisitor/TrashVisitor.java @@ -83,7 +83,7 @@ public class TrashVisitor { public static void main(String[] args) { ArrayList bin = new ArrayList<>(); // ParseTrash still works, without changes: - ParseTrash.fillBin("VTrash.dat", bin); + ParseTrash.fillBin("trashvisitor", "VTrash.dat", bin); // You could even iterate through // a list of visitors! PriceVisitor pv = new PriceVisitor();