10
10
import java .io .File ;
11
11
import java .io .FileInputStream ;
12
12
import java .io .FileOutputStream ;
13
+ import java .io .FilenameFilter ;
13
14
import java .io .IOException ;
14
15
import java .io .InputStreamReader ;
15
16
import java .io .OutputStreamWriter ;
@@ -29,6 +30,7 @@ public void init() {
29
30
files = new HashMap <>();
30
31
31
32
Functions .set ("fopen" , new fopen ());
33
+ Functions .set ("listFiles" , new listFiles ());
32
34
Functions .set ("readBoolean" , new readBoolean ());
33
35
Functions .set ("readByte" , new readByte ());
34
36
Functions .set ("readBytes" , new readBytes ());
@@ -52,6 +54,7 @@ public void init() {
52
54
Functions .set ("writeDouble" , new writeDouble ());
53
55
Functions .set ("writeUTF" , new writeUTF ());
54
56
Functions .set ("writeLine" , new writeLine ());
57
+ Functions .set ("writeText" , new writeText ());
55
58
Functions .set ("flush" , new flush ());
56
59
Functions .set ("fclose" , new fclose ());
57
60
}
@@ -112,6 +115,19 @@ public Value execute(Value... args) {
112
115
protected abstract Value execute (FileInfo fileInfo , Value [] args ) throws IOException ;
113
116
}
114
117
118
+ private static class listFiles extends FileFunction {
119
+ @ Override
120
+ protected Value execute (FileInfo fileInfo , Value [] args ) throws IOException {
121
+ final String [] files = fileInfo .file .list ();
122
+ final int size = files .length ;
123
+ final ArrayValue result = new ArrayValue (size );
124
+ for (int i = 0 ; i < size ; i ++) {
125
+ result .set (i , new StringValue (files [i ]));
126
+ }
127
+ return result ;
128
+ }
129
+ }
130
+
115
131
private static class readBoolean extends FileFunction {
116
132
@ Override
117
133
protected Value execute (FileInfo fileInfo , Value [] args ) throws IOException {
@@ -314,7 +330,16 @@ protected Value execute(FileInfo fileInfo, Value[] args) throws IOException {
314
330
private static class writeLine extends FileFunction {
315
331
@ Override
316
332
protected Value execute (FileInfo fileInfo , Value [] args ) throws IOException {
317
- fileInfo .dos .writeDouble (args [1 ].asNumber ());
333
+ fileInfo .writer .write (args [1 ].asString ());
334
+ fileInfo .writer .newLine ();
335
+ return NumberValue .ONE ;
336
+ }
337
+ }
338
+
339
+ private static class writeText extends FileFunction {
340
+ @ Override
341
+ protected Value execute (FileInfo fileInfo , Value [] args ) throws IOException {
342
+ fileInfo .writer .write (args [1 ].asString ());
318
343
return NumberValue .ONE ;
319
344
}
320
345
}
0 commit comments