Skip to content

Commit 5a25fea

Browse files
committed
Fixed path issue using nio Path
1 parent 06855b9 commit 5a25fea

6 files changed

Lines changed: 16 additions & 13 deletions

File tree

patterns/doubledispatch/DoubleDispatch.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static void main(String[] args) {
5656
ArrayList<Trash> bin = new ArrayList<>();
5757
TrashBinSet bins = new TrashBinSet();
5858
// ParseTrash still works, without changes:
59-
ParseTrash.fillBin("DDTrash.dat", bin);
59+
ParseTrash.fillBin("doubledispatch", "DDTrash.dat", bin);
6060
// Sort from the master bin into the
6161
// individually-typed bins:
6262
bins.sortIntoBins(bin);

patterns/dynatrash/DynaTrash.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class DynaTrash {
4444
@SuppressWarnings("unchecked")
4545
public static void main(String[] args) {
4646
TypeMap<Trash> bin = new TypeMap<>();
47-
ParseTrash.fillBin("Trash.dat",
47+
ParseTrash.fillBin("recycleap", "Trash.dat",
4848
new TypeMapAdapter(bin));
4949
Iterator<Class> keys = bin.keys();
5050
while(keys.hasNext())

patterns/recycleap/RecycleAP.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class RecycleAP {
99
public static void main(String[] args) {
1010
ArrayList<Trash> bin = new ArrayList<>();
1111
// Fill up the Trash bin:
12-
ParseTrash.fillBin("Trash.dat", bin);
12+
ParseTrash.fillBin("recycleap", "Trash.dat", bin);
1313
List<Glass> glassBin = new ArrayList<>();
1414
List<Paper> paperBin = new ArrayList<>();
1515
List<Aluminum> alBin = new ArrayList<>();

patterns/recycleb/RecycleB.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class RecycleB {
4141
static Tbin<Trash> bin = new Tbin<>(Trash.class);
4242
public static void main(String[] args) {
4343
// Fill up the Trash bin:
44-
ParseTrash.fillBin("Trash.dat", bin);
44+
ParseTrash.fillBin("recycleap", "Trash.dat", bin);
4545

4646
TbinList<Trash> trashBins = new TbinList<>();
4747
trashBins.add(new Tbin<>(Aluminum.class));

patterns/trash/ParseTrash.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
package patterns.trash;
66
import java.util.*;
77
import java.io.*;
8+
import java.nio.file.*;
89

910
public class ParseTrash {
1011
public static <T extends Trash> void
11-
fillBin(String filename, Fillable<T> bin) {
12+
fillBin(String dir, String fname, Fillable<T> bin) {
1213
try {
13-
try(BufferedReader data = new BufferedReader(
14-
new FileReader(filename))) {
14+
Path p = FileSystems.getDefault()
15+
.getPath("..", dir, fname);
16+
try(BufferedReader data = Files.newBufferedReader(p)){
1517
String buf;
1618
while((buf = data.readLine())!= null) {
1719
if(buf.trim().length() == 0)
1820
continue; // Skip empty lines
21+
if(buf.startsWith("//"))
22+
continue; // Skip comments
1923
String type = buf.substring(0,
20-
buf.indexOf(':')).trim();
24+
buf.indexOf(':')).trim();
2125
double weight = Double.valueOf(
22-
buf.substring(buf.indexOf(':') + 1)
23-
.trim());
26+
buf.substring(buf.indexOf(':') + 1).trim());
2427
bin.addTrash(Trash.factory(
2528
new Trash.Info(type, weight)));
2629
}
@@ -34,7 +37,7 @@ public class ParseTrash {
3437
}
3538
// Special case to handle ArrayList:
3639
public static <T extends Trash> void
37-
fillBin(String filename, ArrayList<T> bin) {
38-
fillBin(filename, new FillableList<>(bin));
40+
fillBin(String dir, String fname, ArrayList<T> bin) {
41+
fillBin(dir, fname, new FillableList<>(bin));
3942
}
4043
}

patterns/trashvisitor/TrashVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public class TrashVisitor {
8383
public static void main(String[] args) {
8484
ArrayList<Trash> bin = new ArrayList<>();
8585
// ParseTrash still works, without changes:
86-
ParseTrash.fillBin("VTrash.dat", bin);
86+
ParseTrash.fillBin("trashvisitor", "VTrash.dat", bin);
8787
// You could even iterate through
8888
// a list of visitors!
8989
PriceVisitor pv = new PriceVisitor();

0 commit comments

Comments
 (0)