1. 本地文件读取
package
com.jn.scala
import
scala.io.Source
/**
* Created by admin on 2015/11/21.
*/
object
FileOps {
def
main(args: Array[
String
]) {
val
file = Source.fromFile (
"D:
\\
jiangning.txt"
)
//读取文件
for
(line <- file.getLines()){
//对文件中的每一行进行遍历
println(line)
}
file.close()
}
}
//运行结果
good
spark
scala
2.源码分析
object Source extends
scala.AnyRef {
val
DefaultBufSize
: scala.Int = {
/* compiled code */
}
def
stdin : scala.io.BufferedSource = {
/* compiled code */
}
def
fromIterable(iterable : scala.
Iterable
[scala.Char]) : scala.io.Source = {
/* compiled code */
}
def
fromChar(c : scala.Char) : scala.io.Source = {
/* compiled code */
}
def
fromChars(chars : scala.Array[scala.Char]) : scala.io.Source = {
/* compiled code */
}
def
fromString(s : scala.Predef.
String
) : scala.io.Source = {
/* compiled code */
}
def fromFile(name : scala.Predef.String )(implicit codec : scala.io.Codec) : scala.io.BufferedSource = { /* compiled code */
}
def
fromFile(name : scala.Predef.
String
, enc : scala.Predef.
String
) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromFile(uri : java.net.URI)(
implicit
codec : scala.io.Codec) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromFile(uri : java.net.URI, enc : scala.Predef.
String
) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromFile(file : java.io.File)(
implicit
codec : scala.io.Codec) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromFile(file : java.io.File, enc : scala.Predef.
String
) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromFile(file : java.io.File, enc : scala.Predef.
String
, bufferSize : scala.Int) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromFile(file : java.io.File, bufferSize : scala.Int)(
implicit
codec : scala.io.Codec) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromBytes(bytes : scala.Array[scala.Byte])(
implicit
codec : scala.io.Codec) : scala.io.Source = {
/* compiled code */
}
def
fromBytes(bytes : scala.Array[scala.Byte], enc : scala.Predef.
String
) : scala.io.Source = {
/* compiled code */
}
def
fromRawBytes(bytes : scala.Array[scala.Byte]) : scala.io.Source = {
/* compiled code */
}
def
fromURI(uri : java.net.URI)(
implicit
codec : scala.io.Codec) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromURL(s : scala.Predef.
String
, enc : scala.Predef.
String
) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromURL(s : scala.Predef.
String
)(
implicit
codec : scala.io.Codec) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromURL(url : java.net.URL, enc : scala.Predef.
String
) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromURL(url : java.net.URL)(
implicit
codec : scala.io.Codec) : scala.io.BufferedSource = {
/* compiled code */
}
def
createBufferedSource(inputStream : java.io.InputStream, bufferSize : scala.Int = {
/* compiled code */
}, reset : scala.Function0[scala.io.Source] = {
/* compiled code */
}, close : scala.Function0[scala.Unit] = {
/* compiled code */
})(
implicit
codec : scala.io.Codec) : scala.io.BufferedSource = {
/* compiled code */
}
def
fromInputStream(is : java.io.InputStream, enc : scala.Predef.
String
) : scala.io.BufferedSource = {
/* compiled code */
}
def fromInputStream(is : java.io.InputStream)(implicit codec : scala.io.Codec) : scala.io.BufferedSource = { /* compiled code */
}
}
3.这里看到没有进行源码关联,下载源码,选择自己的版本
http://www.scala-lang.org/download/2.11.7.html
解压后,压缩成zip包
4.源码
(1)/** 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)
返回值
BufferedSource
(2)/** This object provides convenience methods to create an iterable
* representation of a source file.
*
*
@author
Burak Emir, Paul Phillips
*/
class
BufferedSource(inputStream: InputStream, bufferSize: Int)(
implicit val
codec: Codec)
extends
Source {
def this
(inputStream: InputStream)(
implicit
codec: Codec) =
this
(inputStream,
DefaultBufSize
)(codec)
def
reader() =
new
InputStreamReader(inputStream, codec.decoder)
def bufferedReader() = new BufferedReader(reader(), bufferSize)
5.读取URL
package
com.jn.scala
import
scala.io.Source
/**
* Created by admin on 2015/11/21.
*/
object
FileOps {
def
main(args: Array[
String
]) {
// val file = Source.fromFile("D:\\jiangning.txt")//读取文件
// for(line <- file.getLines()){//对文件中的每一行进行遍历
// println(line)
// }
// file.close()
val
file = Source.fromURL(
"http://spark.apache.org"
)
//读取文件
for
(line <- file.getLines()){
//对文件中的每一行进行遍历
println(line)
}
file.close()
}
}
6.写文件
package
com.jn.scala
import
java.io.{File, PrintWriter}
import
scala.io.Source
/**
* Created by admin on 2015/11/21.
*/
object
FileOps {
def
main(args: Array[
String
]) {
// val file = Source.fromFile("D:\\jiangning.txt")//读取文件
// for(line <- file.getLines()){//对文件中的每一行进行遍历
// println(line)
// }
// file.close()
// val file = Source.fromURL("http://spark.apache.org")//读取文件
// for(line <- file.getLines()){//对文件中的每一行进行遍历
// println(line)
// }
// file.close()
val
writer =
new
PrintWriter(
new
File(
"scalaFile.txt"
))
for
(i <-
1
to
100
)writer.println(i)
writer.close()
}
}
利用java的写文件进行写入
7.控制台输入
package
com.jn.scala
import
java.io.{File, PrintWriter}
import
scala.io.Source
/**
* Created by admin on 2015/11/21.
*/
object
FileOps {
def
main(args: Array[
String
]) {
// val file = Source.fromFile("D:\\jiangning.txt")//读取文件
// for(line <- file.getLines()){//对文件中的每一行进行遍历
// println(line)
// }
// file.close()
// val file = Source.fromURL("http://spark.apache.org")//读取文件
// for(line <- file.getLines()){//对文件中的每一行进行遍历
// println(line)
// }
// file.close()
// val writer = new PrintWriter(new File("scalaFile.txt"))
// for(i <- 1 to 100)writer.println(i)
// writer.close()
print(
"Please enter your input : "
)
val
line = Console.readLine
println(
"Thanks ,for your help "
+ line)
}
}