|
| 1 | + ---------------------------------- |
| 2 | + jScriptBasic Project Documentation |
| 3 | + ---------------------------------- |
| 4 | + Peter Verhas |
| 5 | + ---------------------------------- |
| 6 | + 2012-12-04 |
| 7 | + ---------------------------------- |
| 8 | + |
| 9 | +ScriptBasic for Java File Handling |
| 10 | + |
| 11 | + ScriptBasic for Java contains an extension class that can be used from BASIC programs |
| 12 | + to perform file operations. This extension class is <<NOT>> registered by default. The embedding |
| 13 | + application has to register this class explicitly calling the method |
| 14 | + |
| 15 | +--- |
| 16 | +interpreter.registerFunctions(FileHandlingFunctions.class); |
| 17 | +--- |
| 18 | + |
| 19 | + The command line version does register this class before executing a script, thus you need not specify |
| 20 | + this class on the command line using the <<<-Dsb4j.extensionclasses>>> command line option. |
| 21 | + |
| 22 | +* Opening and Closing a File |
| 23 | + |
| 24 | + To open a file the function <<<open>>> has to be used. |
| 25 | + |
| 26 | +--- |
| 27 | +file = open(fileName,mode) |
| 28 | +--- |
| 29 | + |
| 30 | + A file can be opened in four different ways. They can be read or write and each can be text or binary. The default is |
| 31 | + read and text. The parameter <<<fileName>>> should specify the name of the file and the parameter <<<mode>>> is a two letter |
| 32 | + text: <<<"rt">>> to read in text mode, <<<"wt">>> write in text mode, <<<"rb">>> read in binary mode, |
| 33 | + <<<"wb">>> write in binary mode. If the mode is read then this is enoug to specify <<<"t">>> to read in text mode, or |
| 34 | + <<<"b">>> to read in binary mode. Furthermore if the mode is read in text mode then an empty string <<<"">>> can be used |
| 35 | + as the <<<mode>>> parameter. |
| 36 | + |
| 37 | + Currently there is no way to open a file in read/write mixed mode. Also the operations you can perform on a text file |
| 38 | + are separated from the operations available for binary file handling. |
| 39 | + |
| 40 | + The return value of the function <<<open>>> is a file handler that the BASIC program should not use in any other |
| 41 | + way than passing to other extension functions that are prepared to accept a file handler as argument. This simplest |
| 42 | + example is he <<<close>>> function: |
| 43 | + |
| 44 | +--- |
| 45 | +close file |
| 46 | +--- |
| 47 | + |
| 48 | + This function closes the file handle. |
| 49 | + |
| 50 | +* Reading a text file |
| 51 | + |
| 52 | + Reading a text file is available line by line. The function to read a line from a text file is <<<readLine>>>: |
| 53 | + |
| 54 | +--- |
| 55 | +line = readLine(file) |
| 56 | +--- |
| 57 | + |
| 58 | + The argument to this function has to be the handle returned by the function <<<open>>>. The function reads a line |
| 59 | + from the file and returns the line as a string without the line termination character or characters. |
| 60 | + |
| 61 | + If there is no more line to read the function returns undefined value that you can check calling the functions |
| 62 | + <<<isDefined>>> or <<<isUndef>>>. |
| 63 | + |
| 64 | +* Writing to text file |
| 65 | + |
| 66 | + Writing to text file is possible using two functions: <<<printfln>>> and <<<printf>>>. Both of the functions |
| 67 | + accept a file handle and s single string argument. The function <<<printf>>> writes the argument string to the |
| 68 | + file. The function <<<printfln>>> writes the argument string to the file and also a line ending. This may |
| 69 | + not be a simple line-feed or carriage return character. The behaviour is operating system dependent, therefore |
| 70 | + it is safer to use <<<printfln file, string>>> than <<<printf file, string +"\n">>>. |
| 71 | + |
| 72 | +* Reading Binary File |
| 73 | + |
| 74 | +* Writing Binary File |
| 75 | + |
| 76 | +* Other File Operations |
| 77 | + |
| 78 | +** Deleting a File |
| 79 | + |
| 80 | + To delete a file you can use the function |
| 81 | + |
| 82 | +--- |
| 83 | +deleteFile fileName |
| 84 | +--- |
| 85 | + |
| 86 | + The argument to the function is the name of the file. |
| 87 | + |
| 88 | +** Listing the Files in a Directory |
| 89 | + |
| 90 | + To get the names of the files that are in a directory you have to use the function |
| 91 | +--- |
| 92 | +fileList = listFiles(directoryName) |
| 93 | +--- |
| 94 | + |
| 95 | + The argument to the function is the name of the directory. The return value is an array of string values |
| 96 | + containing the names of the files that are in the named directory. |
0 commit comments