-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
LocalFile.scala
85 lines (72 loc) · 2.24 KB
/
LocalFile.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.api
class LocalFile(filepath: String) extends File {
private var w: java.io.PrintWriter = null
override def getPrintWriter = w
@throws(classOf[java.io.IOException])
override def getInputStream =
new java.io.FileInputStream(getPath)
@throws(classOf[java.io.IOException])
override def open(mode: FileMode) = {
if (w != null || reader != null)
throw new java.io.IOException(
"Attempted to open an already open file")
mode match {
case FileMode.Read =>
pos = 0
eof = false
reader = new java.io.BufferedReader(
new java.io.InputStreamReader(
new java.io.BufferedInputStream(
new java.io.FileInputStream(new java.io.File(filepath)))))
this.mode = mode
case FileMode.Write =>
w = new java.io.PrintWriter(new java.io.FileWriter(filepath))
this.mode = mode
case FileMode.Append =>
w = new java.io.PrintWriter(new java.io.FileWriter(filepath, true))
this.mode = mode
case FileMode.None =>
sys.error("file is not open")
}
}
@throws(classOf[java.io.IOException])
override def print(str: String) {
if (w == null)
throw new java.io.IOException("Attempted to print to an unopened File")
w.print(str)
}
@throws(classOf[java.io.IOException])
override def println(line: String) {
if (w == null)
throw new java.io.IOException("Attempted to println to an unopened File")
w.println(line)
}
@throws(classOf[java.io.IOException])
override def println() {
if (w == null)
throw new java.io.IOException("Attempted to println to an unopened File")
w.println()
}
override def flush() {
if (w != null)
w.flush()
}
@throws(classOf[java.io.IOException])
override def close(ok: Boolean) {
mode match {
case FileMode.Write | FileMode.Append =>
w.close()
w = null
case FileMode.Read =>
reader.close()
reader = null
case FileMode.None =>
}
mode = FileMode.None
}
override def getAbsolutePath =
new java.io.File(filepath).getAbsolutePath
override def getPath =
new java.io.File(filepath).getPath
}