1.读取行

要读取文件的所有行,可以调用scala.io.Source对象的getLines方法:

import scala.io.Source
val source = Source.fromFile("myfile.txt", "UTF-8")
val lineIterator = source.getLines
val lines1 =lineIterator.toArray
val lines2 = lineIterator.toBuffer
//将文件内容读成字符串
val lines = source.mkString
source.close 

2.读取字符

val iter = source.buffered  

while(iter.hasNext){
if(iter.next == '王'){
println("wang")
}else{
println("-")
}
}  

3.读取词法单元或数字

val iter 2= source.mkString.split("\\s+")  

val num = for(w <- iter2) yield w.toDouble  

for(i <- num) println(i) 

4.从URL或其它资源读取

val source1 = Source.fromURL("http://baidu.com")//URL读取
val source2 = Source.fromString("hello")//读取给定的字符串-多用于调试
import scala.io.StdIn
val ms=StdIn.readLine()  

5.读取二进制文件

import java.io.{File, FileInputStream}
val file = new File(" ")
val in = new FileInputStream(file)
val bytes = new Array[Byte](file.length.toInt)
in.read(bytes)
in.close() 

6.写入文本

val out = new PrintWriter("numbers.txt")
for (i <- 1 to 100) {
out.println(i)
out.print(f"$quantity%6d $price%10.2f")
}
out.close() 

7.访问目录

import java.nio.file._
val dirname = "/home/cay/scala-impatient/code"
val entries = Files.walk(Paths.get(dirname)) // or Files.list
try {
entries.forEach(p => println(p))
} finally {
entries.close()

8.序列化

@SerialVersionUID(42L) class Person extends Serializable{}
class Person extends Serializable {
private val friends = new ArrayBuffer[Person] // OK—ArrayBuffer is serializable
}
val fred = new Person(...)
import java.io._
val out = new ObjectOutputStream(new FileOutputStream("/tmp/test.obj"))
out.writeObject(fred)
out.close()
val in = new ObjectInputStream(new FileInputStream("/tmp/test.obj"))
val savedFred = in.readObject().asInstanceOf[Person]

9.scala脚本与shell命令

import scala.sys.process._
"ls -al ..".!
("ls -al /" #| "grep u").!
#输出重定向到文件
("ls -al /" #> new File("filelist.txt")).!
#追加到末尾
("ls -al /etc" #>> new File("filelist.txt")).!
#把某个文件的内容作为输入
("grep u" #< new File("filelist.txt")).!
#从URL重定向输入
("grep Scala" #< new URL("http://horstmann.com/index.html")).!

scala2.11读取文件的更多相关文章

  1. centos+scala2.11.4+hadoop2.3+spark1.3.1环境搭建

    一.Java安装 1.安装包准备: 首先到官网下载jdk,http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads- ...

  2. Android从assets目录下读取文件相关

    有一个需求是app的帮助文档是word格式,ios可以直接用webview加载word显示,Android不行.而美工不配合转换成图片,前端没时间把word写成html 没办法,自己搞. 步骤: 1. ...

  3. java 正则表达式的应用:读取文件,获取其中的电话号码

    1.正则表达式 正则表达式,又称正规表示法.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式使用单个字符串来描 ...

  4. java读取文件

       一个字节一个字节地读取                  File file =          InputStream inputStream =                       ...

  5. Java 读取文件到字符串

    Java的io操作比较复杂 package cn.outofmemory.util; import java.io.BufferedReader; import java.io.FileInputSt ...

  6. java 读取文件的常用方式

    1.读取: public class ReadFromFile { /** * 以字节为单位读取文件,常用于读二进制文件,如图片.声音.影像等文件. */ public static void rea ...

  7. java中读取文件以及向文件中追加数据的总结

    package gys; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; imp ...

  8. QT 读取文件夹下所有文件(超级简单的方法,不需要QDirIterator)

    之前,用标准C++写过读取文件夹.现在用QT重写代码,顺便看了下QT如何实现,还是相当简单的.主要用到QDir,详细文档可见这里 A program that lists all the files ...

  9. 【转】Java读取文件方法大全

    本文转自:http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html#undefined 目录: 按字节读取文件内容 按字符读取文 ...

随机推荐

  1. PAT甲级——A1032 Sharing

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  2. line-height:2和line-height:2em的区别,它们是有区别的

    line-height:2是2倍的意思,如果内部有不同大小文字的情况下,以最大文字为倍数. line-height:2em也是2倍文字大小的意思,但如果内部有大文字,它还是会以父容 器的大小来计算. ...

  3. 【DM8168学习笔记4】ezsdk安装过程记录

    安装文件 ezsdk_dm816x-evm_5_05_02_00_setuplinux DM8168-EZSDK文件结构如图所示.               (图片来自:http://process ...

  4. combobox 的onLoadSuccess执行两次解决办法和 取值赋值

    加红色字部分 jsp <input class="easyui-combobox" id="keshi" name="keshi" v ...

  5. java基础之集合(Set,Map,List)总结

    一.Collection接口 1.Collection接口两个子接口     -List接口         -ArrayList:         -Vector:         -LinkedL ...

  6. 2019-8-31-dotnet-控制台读写-Sqlite-提示-no-such-table-找不到文件

    title author date CreateTime categories dotnet 控制台读写 Sqlite 提示 no such table 找不到文件 lindexi 2019-08-3 ...

  7. Trees in a Wood UVA - 10214 欧拉函数模板

    太坑惹,,,没用longlong各种WA #include <iostream> #include <string.h> #include <cstdio> #in ...

  8. MySQL事务、锁机制、查询缓存

    MySQL事务 何为事务? 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 一个事务可以是一条SQL语句,一组SQL语句或整个程序. 事务的特性: 事 ...

  9. vim编辑器操作①

    Linux文本编辑器: 行编辑器:sed 全屏编辑器:nano,vi/vim 本文主要介绍说明vim编辑器的相关使用: 其有三种模式,即: 编辑模式(默认模式).插入模式(输入模式).末行模式(内置的 ...

  10. TZ_09_MyBatis的pageHelper

    1.分页操作使用MyBatis的PageHelper 1>导入pageHelper的坐标 <dependency> <groupId>com.github.pagehel ...