Scala 文件写入

来源:互联网 发布:毕向东java基础35天 编辑:程序博客网 时间:2024/06/05 17:39

Scala 文件写入

   //文件写入    val writer = new PrintWriter(new File("learningScala.txt"))    for(i <- 1 to 100)      writer.println(i)    writer.close()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

报错:

Error:(28, 38) not found: type File    val writer = new PrintWriter(new File("learningScala.txt"))                                     ^
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  • 解决方法:

添加Java包:

import java.io.PrintWriterimport java.io.File
  • 1
  • 2
  • 1
  • 2

运行成功截图: 
这里写图片描述
PrintWriter()方法源码如下:

    /**     * Creates a new PrintWriter, without automatic line flushing, with the     * specified file.  This convenience constructor creates the necessary     * intermediate {@link java.io.OutputStreamWriter OutputStreamWriter},     * which will encode characters using the {@linkplain     * java.nio.charset.Charset#defaultCharset() default charset} for this     * instance of the Java virtual machine.     *     * @param  file     *         The file to use as the destination of this writer.  If the file     *         exists then it will be truncated to zero size; otherwise, a new     *         file will be created.  The output will be written to the file     *         and is buffered.     *     * @throws  FileNotFoundException     *          If the given file object does not denote an existing, writable     *          regular file and a new regular file of that name cannot be     *          created, or if some other error occurs while opening or     *          creating the file     *     * @throws  SecurityException     *          If a security manager is present and {@link     *          SecurityManager#checkWrite checkWrite(file.getPath())}     *          denies write access to the file     *     * @since  1.5     */    public PrintWriter(File file) throws FileNotFoundException {        this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),             false);    }
  • 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
  • 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

控制台操作

  //控制台交互    print("Please enter your input:")    val line=Console.readLine()    println("Thanks,you just typed:"+line)
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

这里写图片描述

  • 附录:Console - package scala
/*                     __                                               *\**     ________ ___   / /  ___     Scala API                            ****    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             ****  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **** /____/\___/_/ |_/____/_/ | |                                         ****                          |/                                          **\*                                                                      */package scalaimport java.io.{ BufferedReader, InputStream, InputStreamReader, OutputStream, PrintStream, Reader }import scala.io.{ AnsiColor, StdIn }import scala.util.DynamicVariable/** Implements functionality for *  printing Scala values on the terminal as well as reading specific values. *  Also defines constants for marking up text on ANSI terminals. * *  @author  Matthias Zenger *  @version 1.0, 03/09/2003 */object Console extends DeprecatedConsole with AnsiColor {  private val outVar = new DynamicVariable[PrintStream](java.lang.System.out)  private val errVar = new DynamicVariable[PrintStream](java.lang.System.err)  private val inVar  = new DynamicVariable[BufferedReader](    new BufferedReader(new InputStreamReader(java.lang.System.in)))  protected def setOutDirect(out: PrintStream): Unit  = outVar.value = out  protected def setErrDirect(err: PrintStream): Unit  = errVar.value = err  protected def setInDirect(in: BufferedReader): Unit = inVar.value = in  /** The default output, can be overridden by `setOut` */  def out = outVar.value  /** The default error, can be overridden by `setErr` */  def err = errVar.value  /** The default input, can be overridden by `setIn` */  def in = inVar.value  /** Sets the default output stream for the duration   *  of execution of one thunk.   *   *  @example {{{   *  withOut(Console.err) { println("This goes to default _error_") }   *  }}}   *   *  @param out the new output stream.   *  @param thunk the code to execute with   *               the new output stream active   *  @return the results of `thunk`   *  @see `withOut[T](out:OutputStream)(thunk: => T)`   */  def withOut[T](out: PrintStream)(thunk: =>T): T =    outVar.withValue(out)(thunk)  /** Sets the default output stream for the duration   *  of execution of one thunk.   *   *  @param out the new output stream.   *  @param thunk the code to execute with   *               the new output stream active   *  @return the results of `thunk`   *  @see `withOut[T](out:PrintStream)(thunk: => T)`   */  def withOut[T](out: OutputStream)(thunk: =>T): T =    withOut(new PrintStream(out))(thunk)  /** Set the default error stream for the duration   *  of execution of one thunk.   *  @example {{{   *  withErr(Console.out) { println("This goes to default _out_") }   *  }}}   *   *  @param err the new error stream.   *  @param thunk the code to execute with   *               the new error stream active   *  @return the results of `thunk`   *  @see `withErr[T](err:OutputStream)(thunk: =>T)`   */  def withErr[T](err: PrintStream)(thunk: =>T): T =    errVar.withValue(err)(thunk)  /** Sets the default error stream for the duration   *  of execution of one thunk.   *   *  @param err the new error stream.   *  @param thunk the code to execute with   *               the new error stream active   *  @return the results of `thunk`   *  @see `withErr[T](err:PrintStream)(thunk: =>T)`   */  def withErr[T](err: OutputStream)(thunk: =>T): T =    withErr(new PrintStream(err))(thunk)  /** Sets the default input stream for the duration   *  of execution of one thunk.   *   *  @example {{{   *  val someFile:Reader = openFile("file.txt")   *  withIn(someFile) {   *    // Reads a line from file.txt instead of default input   *    println(readLine)   *  }   *  }}}   *   *  @param thunk the code to execute with   *               the new input stream active   *   * @return the results of `thunk`   * @see `withIn[T](in:InputStream)(thunk: =>T)`   */  def withIn[T](reader: Reader)(thunk: =>T): T =    inVar.withValue(new BufferedReader(reader))(thunk)  /** Sets the default input stream for the duration   *  of execution of one thunk.   *   *  @param in the new input stream.   *  @param thunk the code to execute with   *               the new input stream active   * @return the results of `thunk`   * @see `withIn[T](reader:Reader)(thunk: =>T)`   */  def withIn[T](in: InputStream)(thunk: =>T): T =    withIn(new InputStreamReader(in))(thunk)  /** Prints an object to `out` using its `toString` method.   *   *  @param obj the object to print; may be null.   */  def print(obj: Any) {    out.print(if (null == obj) "null" else obj.toString())  }  /** Flushes the output stream. This function is required when partial   *  output (i.e. output not terminated by a newline character) has   *  to be made visible on the terminal.   */  def flush() { out.flush() }  /** Prints a newline character on the default output.   */  def println() { out.println() }  /** Prints out an object to the default output, followed by a newline character.   *   *  @param x the object to print.   */  def println(x: Any) { out.println(x) }  /** Prints its arguments as a formatted string to the default output,   *  based on a string pattern (in a fashion similar to printf in C).   *   *  The interpretation of the formatting patterns is described in   *  <a href="" target="contentFrame" class="java/util/Formatter">   *  `java.util.Formatter`</a>.   *   *  @param text the pattern for formatting the arguments.   *  @param args the arguments used to instantiating the pattern.   *  @throws java.lang.IllegalArgumentException if there was a problem with the format string or arguments   */  def printf(text: String, args: Any*) { out.print(text format (args : _*)) }}private[scala] abstract class DeprecatedConsole {  self: Console.type =>  /** Internal usage only. */  protected def setOutDirect(out: PrintStream): Unit  protected def setErrDirect(err: PrintStream): Unit  protected def setInDirect(in: BufferedReader): Unit  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readBoolean(): Boolean                     = StdIn.readBoolean()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readByte(): Byte                           = StdIn.readByte()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readChar(): Char                           = StdIn.readChar()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readDouble(): Double                       = StdIn.readDouble()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readFloat(): Float                         = StdIn.readFloat()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readInt(): Int                             = StdIn.readInt()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readLine(): String                         = StdIn.readLine()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readLine(text: String, args: Any*): String = StdIn.readLine(text, args: _*)  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readLong(): Long                           = StdIn.readLong()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readShort(): Short                         = StdIn.readShort()  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readf(format: String): List[Any]           = StdIn.readf(format)  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readf1(format: String): Any                = StdIn.readf1(format)  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readf2(format: String): (Any, Any)         = StdIn.readf2(format)  @deprecated("Use the method in scala.io.StdIn", "2.11.0") def readf3(format: String): (Any, Any, Any)    = StdIn.readf3(format)  /** Sets the default output stream.   *   *  @param out the new output stream.   */  @deprecated("Use withOut", "2.11.0") def setOut(out: PrintStream): Unit = setOutDirect(out)  /** Sets the default output stream.   *   *  @param out the new output stream.   */  @deprecated("Use withOut", "2.11.0") def setOut(out: OutputStream): Unit = setOutDirect(new PrintStream(out))  /** Sets the default error stream.   *   *  @param err the new error stream.   */  @deprecated("Use withErr", "2.11.0") def setErr(err: PrintStream): Unit = setErrDirect(err)  /** Sets the default error stream.   *   *  @param err the new error stream.   */  @deprecated("Use withErr", "2.11.0") def setErr(err: OutputStream): Unit = setErrDirect(new PrintStream(err))  /** Sets the default input stream.   *   *  @param reader specifies the new input stream.   */  @deprecated("Use withIn", "2.11.0") def setIn(reader: Reader): Unit = setInDirect(new BufferedReader(reader))  /** Sets the default input stream.   *   *  @param in the new input stream.   */  @deprecated("Use withIn", "2.11.0") def setIn(in: InputStream): Unit = setInDirect(new BufferedReader(new InputStreamReader(in)))}
  • 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
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 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
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223

附录一:本节所有程序源码

package kmust.hjr.learningScala18import java.io.PrintWriterimport java.io.Fileimport scala.io.Source/** * Created by Administrator on 2015/10/16. */object FileOps {  def main(args:Array[String]){    //文件读取    val file=Source.fromFile("E:\\scalaIO.txt")    for(line <- file.getLines)    {      println(line)    }    file.close    //网络资源读取    //val webFile=Source.fromURL("http://www.baidu.com/")    val webFile=Source.fromURL("http://spark.apache.org")    webFile.foreach(print)    webFile.close()    //文件写入    val writer = new PrintWriter(new File("learningScala.txt"))    for(i <- 1 to 100)      writer.println(i)    writer.close()    //控制台交互    print("Please enter your input:")    val line=Console.readLine()    println("Thanks,you just typed:"+line)  }}
  • 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
  • 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

附录二: Source.scala源码

/*                     __                                               *\**     ________ ___   / /  ___     Scala API                            ****    / __/ __// _ | / /  / _ |    (c) 2003-2013, LAMP/EPFL             ****  __\ \/ /__/ __ |/ /__/ __ |    http://scala-lang.org/               **** /____/\___/_/ |_/____/_/ | |                                         ****                          |/                                          **\*                                                                      */package scalapackage ioimport scala.collection.AbstractIteratorimport java.io.{ FileInputStream, InputStream, PrintStream, File => JFile }import java.net.{ URI, URL }/** This object provides convenience methods to create an iterable *  representation of a source file. * *  @author  Burak Emir, Paul Phillips *  @version 1.0, 19/08/2004 */object Source {  val DefaultBufSize = 2048  /** Creates a `Source` from System.in.   */  def stdin = fromInputStream(System.in)  /** Creates a Source from an Iterable.   *   *  @param    iterable  the Iterable   *  @return   the Source   */  def fromIterable(iterable: Iterable[Char]): Source = new Source {    val iter = iterable.iterator  } withReset(() => fromIterable(iterable))  /** Creates a Source instance from a single character.   */  def fromChar(c: Char): Source = fromIterable(Array(c))  /** creates Source from array of characters, with empty description.   */  def fromChars(chars: Array[Char]): Source = fromIterable(chars)  /** creates Source from a String, with no description.   */  def fromString(s: String): Source = fromIterable(s)  /** creates Source from file with given name, setting its description to   *  filename.   */  def fromFile(name: String)(implicit codec: Codec): BufferedSource =    fromFile(new JFile(name))(codec)  /** creates Source from file with given name, using given encoding, setting   *  its description to filename.   */  def fromFile(name: String, enc: String): BufferedSource =    fromFile(name)(Codec(enc))  /** creates `ource` from file with given file `URI`.   */  def fromFile(uri: URI)(implicit codec: Codec): BufferedSource =    fromFile(new JFile(uri))(codec)  /** creates Source from file with given file: URI   */  def fromFile(uri: URI, enc: String): BufferedSource =    fromFile(uri)(Codec(enc))  /** creates Source from file, using default character encoding, setting its   *  description to filename.   */  def fromFile(file: JFile)(implicit codec: Codec): BufferedSource =    fromFile(file, Source.DefaultBufSize)(codec)  /** same as fromFile(file, enc, Source.DefaultBufSize)   */  def fromFile(file: JFile, enc: String): BufferedSource =    fromFile(file)(Codec(enc))  def fromFile(file: JFile, enc: String, bufferSize: Int): BufferedSource =    fromFile(file, bufferSize)(Codec(enc))  /** Creates Source from `file`, using given character encoding, setting   *  its description to filename. Input is buffered in a buffer of size   *  `bufferSize`.   */  def fromFile(file: JFile, bufferSize: Int)(implicit codec: Codec): BufferedSource = {    val inputStream = new FileInputStream(file)    createBufferedSource(      inputStream,      bufferSize,      () => fromFile(file, bufferSize)(codec),      () => inputStream.close()    )(codec) withDescription ("file:" + file.getAbsolutePath)  }  /** Create a `Source` from array of bytes, decoding   *  the bytes according to codec.   *   *  @return      the created `Source` instance.   */  def fromBytes(bytes: Array[Byte])(implicit codec: Codec): Source =    fromString(new String(bytes, codec.name))  def fromBytes(bytes: Array[Byte], enc: String): Source =    fromBytes(bytes)(Codec(enc))  /** Create a `Source` from array of bytes, assuming   *  one byte per character (ISO-8859-1 encoding.)   */  def fromRawBytes(bytes: Array[Byte]): Source =    fromString(new String(bytes, Codec.ISO8859.name))  /** creates `Source` from file with given file: URI   */  def fromURI(uri: URI)(implicit codec: Codec): BufferedSource =    fromFile(new JFile(uri))(codec)  /** same as fromURL(new URL(s))(Codec(enc))   */  def fromURL(s: String, enc: String): BufferedSource =    fromURL(s)(Codec(enc))  /** same as fromURL(new URL(s))   */  def fromURL(s: String)(implicit codec: Codec): BufferedSource =    fromURL(new URL(s))(codec)  /** same as fromInputStream(url.openStream())(Codec(enc))   */  def fromURL(url: URL, enc: String): BufferedSource =    fromURL(url)(Codec(enc))  /** same as fromInputStream(url.openStream())(codec)   */  def fromURL(url: URL)(implicit codec: Codec): BufferedSource =    fromInputStream(url.openStream())(codec)  /** Reads data from inputStream with a buffered reader, using the encoding   *  in implicit parameter codec.   *   *  @param  inputStream  the input stream from which to read   *  @param  bufferSize   buffer size (defaults to Source.DefaultBufSize)   *  @param  reset        a () => Source which resets the stream (if unset, reset() will throw an Exception)   *  @param  close        a () => Unit method which closes the stream (if unset, close() will do nothing)   *  @param  codec        (implicit) a scala.io.Codec specifying behavior (defaults to Codec.default)   *  @return              the buffered source   */  def createBufferedSource(    inputStream: InputStream,    bufferSize: Int = DefaultBufSize,    reset: () => Source = null,    close: () => Unit = null  )(implicit codec: Codec): BufferedSource = {    // workaround for default arguments being unable to refer to other parameters    val resetFn = if (reset == null) () => createBufferedSource(inputStream, bufferSize, reset, close)(codec) else reset    new BufferedSource(inputStream, bufferSize)(codec) withReset resetFn withClose close  }  def fromInputStream(is: InputStream, enc: String): BufferedSource =    fromInputStream(is)(Codec(enc))  def fromInputStream(is: InputStream)(implicit codec: Codec): BufferedSource =    createBufferedSource(is, reset = () => fromInputStream(is)(codec), close = () => is.close())(codec)}/** An iterable representation of source data. *  It may be reset with the optional `reset` method. * *  Subclasses must supply [[scala.io.Source@iter the underlying iterator]]. * *  Error handling may be customized by overriding the [[scala.io.Source@report report]] method. * *  The [[scala.io.Source@ch current input]] and [[scala.io.Source@pos position]], *  as well as the [[scala.io.Source@next next character]] methods delegate to *  [[scala.io.Source$Positioner the positioner]]. * *  The default positioner encodes line and column numbers in the position passed to `report`. *  This behavior can be changed by supplying a *  [[scala.io.Source@withPositioning(pos:Source.this.Positioner):Source.this.type custom positioner]]. * *  @author  Burak Emir *  @version 1.0 */abstract class Source extends Iterator[Char] {  /** the actual iterator */  protected val iter: Iterator[Char]  // ------ public values  /** description of this source, default empty */  var descr: String = ""  var nerrors = 0  var nwarnings = 0  private def lineNum(line: Int): String = (getLines() drop (line - 1) take 1).mkString  class LineIterator extends AbstractIterator[String] with Iterator[String] {    private[this] val sb = new StringBuilder    lazy val iter: BufferedIterator[Char] = Source.this.iter.buffered    def isNewline(ch: Char) = ch == '\r' || ch == '\n'    def getc() = iter.hasNext && {      val ch = iter.next()      if (ch == '\n') false      else if (ch == '\r') {        if (iter.hasNext && iter.head == '\n')          iter.next()        false      }      else {        sb append ch        true      }    }    def hasNext = iter.hasNext    def next = {      sb.clear()      while (getc()) { }      sb.toString    }  }  /** Returns an iterator who returns lines (NOT including newline character(s)).   *  It will treat any of \r\n, \r, or \n as a line separator (longest match) - if   *  you need more refined behavior you can subclass Source#LineIterator directly.   */  def getLines(): Iterator[String] = new LineIterator()  /** Returns `'''true'''` if this source has more characters.   */  def hasNext = iter.hasNext  /** Returns next character.   */  def next(): Char = positioner.next()  class Positioner(encoder: Position) {    def this() = this(RelaxedPosition)    /** the last character returned by next. */    var ch: Char = _    /** position of last character returned by next */    var pos = 0    /** current line and column */    var cline = 1    var ccol = 1    /** default col increment for tabs '\t', set to 4 initially */    var tabinc = 4    def next(): Char = {      ch = iter.next()      pos = encoder.encode(cline, ccol)      ch match {        case '\n' =>          ccol = 1          cline += 1        case '\t' =>          ccol += tabinc        case _ =>          ccol += 1      }      ch    }  }  /** A Position implementation which ignores errors in   *  the positions.   */  object RelaxedPosition extends Position {    def checkInput(line: Int, column: Int): Unit = ()  }  object RelaxedPositioner extends Positioner(RelaxedPosition) { }  object NoPositioner extends Positioner(Position) {    override def next(): Char = iter.next()  }  def ch = positioner.ch  def pos = positioner.pos  /** Reports an error message to the output stream `out`.   *   *  @param pos the source position (line/column)   *  @param msg the error message to report   *  @param out PrintStream to use (optional: defaults to `Console.err`)   */  def reportError(    pos: Int,    msg: String,    out: PrintStream = Console.err)  {    nerrors += 1    report(pos, msg, out)  }  private def spaces(n: Int) = List.fill(n)(' ').mkString  /**   *  @param pos the source position (line/column)   *  @param msg the error message to report   *  @param out PrintStream to use   */  def report(pos: Int, msg: String, out: PrintStream) {    val line  = Position line pos    val col   = Position column pos    out println "%s:%d:%d: %s%s%s^".format(descr, line, col, msg, lineNum(line), spaces(col - 1))  }  /**   *  @param pos the source position (line/column)   *  @param msg the warning message to report   *  @param out PrintStream to use (optional: defaults to `Console.out`)   */  def reportWarning(    pos: Int,    msg: String,    out: PrintStream = Console.out)  {    nwarnings += 1    report(pos, "warning! " + msg, out)  }  private[this] var resetFunction: () => Source = null  private[this] var closeFunction: () => Unit = null  private[this] var positioner: Positioner = RelaxedPositioner  def withReset(f: () => Source): this.type = {    resetFunction = f    this  }  def withClose(f: () => Unit): this.type = {    closeFunction = f    this  }  def withDescription(text: String): this.type = {    descr = text    this  }  /** Change or disable the positioner. */  def withPositioning(on: Boolean): this.type = {    positioner = if (on) RelaxedPositioner else NoPositioner    this  }  def withPositioning(pos: Positioner): this.type = {    positioner = pos    this  }  /** The close() method closes the underlying resource. */  def close() {    if (closeFunction != null) closeFunction()  }  /** The reset() method creates a fresh copy of this Source. */  def reset(): Source =    if (resetFunction != null) resetFunction()    else throw new UnsupportedOperationException("Source's reset() method was not set.")}
  • 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
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 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
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365

附录(照例结尾):

这里写图片描述

1
原创粉丝点击