Java 學習記錄83 — I/O FileReader and Closeable — 2/7

張小雄
11 min readOct 13, 2021

--

承 82 — Introduction to I/O

Locations.java

static {        Scanner locScanner = null;        try {
locScanner = new Scanner(new FileReader("src/introduceIo/locations.txt"));
locScanner.useDelimiter(",");
while (locScanner.hasNext()) {
int locInt = locScanner.nextInt();
locScanner.skip(locScanner.delimiter());
String description = locScanner.nextLine();
System.out.println("Imported loc: " + locInt + ": " + description);
Map<String, Integer> tempExit = new HashMap<>();
locations.put(locInt, new Location(locInt, description, tempExit));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (locScanner != null) {
locScanner.close();
;
}
}
// before FileWriter
// Map<String, Integer> tempExit = new HashMap<String, Integer>();
// locations.put(0, new Location(0, "You are sitting in front of a computer learning Java", null));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("W", 2);
// tempExit.put("E", 3);
// tempExit.put("S", 4);
// tempExit.put("N", 5);
// locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building", tempExit));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("N", 5);
// locations.put(2, new Location(2, "You are at the top of a hill", tempExit));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("W", 1);
// locations.put(3, new Location(3, "You are inside a building, a well house for a small spring", tempExit));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("N", 1);
// tempExit.put("W", 2);
// locations.put(4, new Location(4, "You are in a valley beside a stream", tempExit));
//
// tempExit = new HashMap<String, Integer>();
// tempExit.put("S", 1);
// tempExit.put("W", 2);
// locations.put(5, new Location(5, "You are in the forest", tempExit));
}

因為我們要從檔案讀取資料

就不需要自己寫了,把下面都注掉

現在寫出了讀 locations.txt

補充:

java.util.Scanner.skip()

java.util.Scanner.useDelimiter()

接著寫讀 directions.txt

static {        // read locations.txt
Scanner locScanner = null;
try {
locScanner = new Scanner(new FileReader("src/introduceIo/locations.txt"));
locScanner.useDelimiter(",");
while (locScanner.hasNext()) {
int locInt = locScanner.nextInt();
locScanner.skip(locScanner.delimiter());
String description = locScanner.nextLine();
System.out.println("Imported loc: " + locInt + ": " + description);
Map<String, Integer> tempExit = new HashMap<>();
locations.put(locInt, new Location(locInt, description, tempExit));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (locScanner != null) {
locScanner.close();
}
}
// read directions.txt
Scanner dirScanner = null;
try {
dirScanner = new Scanner(new FileReader("src/introduceIo/directions.txt"));
dirScanner.useDelimiter(",");
while (dirScanner.hasNext()) {
int dirInt = dirScanner.nextInt();
dirScanner.skip(dirScanner.delimiter());
String dirDescription = dirScanner.next();
dirScanner.skip(dirScanner.delimiter());
String locationIdStr = dirScanner.nextLine();
int locationIdInt = Integer.parseInt(locationIdStr);
locations.get(dirInt).addExit(dirDescription,locationIdInt );
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dirScanner != null) {
dirScanner.close();
;
}
}
}

Location.java 新增出口功能 addExit()

package introduceIo;import java.util.HashMap;
import java.util.Map;
public class Location {
private final int locationID;
private final String description;
private final Map<String, Integer> exits;
public Location(int locationID, String description, Map<String, Integer> exits) {
this.locationID = locationID;
this.description = description;
if (exits != null) {
this.exits = new HashMap<String, Integer>(exits);
} else {
this.exits = new HashMap<String, Integer>();
}
this.exits.put("Q", 0);
}
public int getLocationID() {
return locationID;
}
public String getDescription() {
return description;
}
public Map<String, Integer> getExits() {
return new HashMap<String, Integer>(exits);
}
protected void addExit(String direction, int location) {
exits.put(direction, location);
}
}

第二個讀檔主要是完成下方這個功能

//        tempExit = new HashMap<String, Integer>();
// tempExit.put("W", 2);
// tempExit.put("E", 3);
// tempExit.put("S", 4);
// tempExit.put("N", 5);
// locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building", tempExit));

從手寫改成從文件裡裡讀

static {        // read locations.txt
Scanner scanner = null;
try {
scanner = new Scanner(new FileReader("src/introduceIo/locations.txt"));
scanner.useDelimiter(",");
while (scanner.hasNext()) {
int locInt = scanner.nextInt();
scanner.skip(scanner.delimiter());
String description = scanner.nextLine();
// System.out.println("Imported loc: " + locInt + ": " + description);
Map<String, Integer> tempExit = new HashMap<>();
locations.put(locInt, new Location(locInt, description, tempExit));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
// read directions.txt - version2
try {
scanner = new Scanner(new BufferedReader(new FileReader("src/introduceIo/directions.txt")));
scanner.useDelimiter(",");
while (scanner.hasNextLine()) {
int loc = scanner.nextInt();
scanner.skip(scanner.delimiter());
String direction = scanner.next();
scanner.skip(scanner.delimiter());
String dest = scanner.next();
int destination = Integer.parseInt(dest);
System.out.println(loc + ": " + direction + ": " + destination);
Location location = locations.get(loc);
location.addExit(direction, destination);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (scanner != null) {
scanner.close();
}
}
}

這次第二種寫法,用的是 BufferReader()

本篇代碼

--

--

張小雄
張小雄

Written by 張小雄

記錄成為軟體工程師的過程

No responses yet