scala 写入文件

Today we will look into Scala File IO operations. File operations mainly include reading data from files or writing data into files. Here we will look into Scala read file and Scala write file programs.

今天,我们将研究Scala File IO操作。 文件操作主要包括从文件读取数据或将数据写入文件。 在这里,我们将研究Scala读取文件和Scala写入文件程序。

Scala文件IO (Scala File IO)

Scala读取文件 (Scala Read File)

We can use scala.io.Source to read data from a file. For reading a file, we have created a test file with below content.

我们可以使用scala.io.Source从文件中读取数据。 为了读取文件,我们创建了一个包含以下内容的测试文件。

data.txt

data.txt

JournalDev is a great platform for Java Developers.
JournalDev is online log of Pankaj Kumar.

Here is a simple program where we are using Scala Source class to read file data to a String and then split it using regular expression to a Map. Finally we are printing the count of JournalDev in the file content.

这是一个简单的程序,其中我们使用Scala Source类将文件数据读取为String,然后使用正则表达式将其拆分为Map。 最后,我们在文件内容中打印JournalDev的计数。

Wordcount.scala

Wordcount.scala

import scala.io.Sourceobject Wordcount {def main(args:Array[String]) {println(Source.fromFile("data.txt")) // returns scala.io.BufferedSource non-empty iterator instanceval s1 = Source.fromFile("data.txt").mkString; //returns the file data as Stringprintln(s1)//splitting String data with white space and calculating the number of occurrence of each word in the file  val counts = s1.split("\\s+").groupBy(x=>x).mapValues(x=>x.length) println(counts)println("Count of JournalDev word:"+counts("JournalDev"))}}

Below image shows the execution of Scala read file program and output.

下图显示了Scala读取文件程序的执行和输出。

Word count line by line: Sometimes there arises a need to process each line rather than the whole contents of the file. This can be achieved through the getLines method. For example below code;

逐行单词计数 :有时需要处理每一行而不是文件的全部内容。 这可以通过getLines方法实现。 例如下面的代码;

println(Source.fromFile("data.txt").getLines())Source.fromFile("data.txt").getLines.foreach { x => println(x) };

will produce following output;

将产生以下输出;

non-empty iterator
JournalDev is a great platform for Java Developers.
JournalDev is online log of Pankaj Kumar.

Now let us see how to extract specific set of lines.

现在让我们看看如何提取特定的线集。

Source.fromFile("data.txt").getLines.take(1).foreach { x => println(x) };
Source.fromFile("data.txt").getLines.slice(0, 1).foreach { x => println(x) };

take(n) method is used to Select first n values of the iterator where slice(from, until) returns part of the iterator where “from” index is the part of slice and “until” index is not. So both the lines in above code snippet are doing the same thing.

take(n)方法用于选择迭代器的前n个值,其中slice(from, until)返回迭代器的一部分,其中“ from”索引是slice的一部分,而“ until”索引不是。 因此,以上代码片段中的两行都在做相同的事情。

Scala写入文件 (Scala Write to File)

Scala standard library doesn’t contain any classes to write files, so we will use Java IO classes for scala write to file. Below is a simple program showing how to write files in scala.

Scala标准库不包含任何用于写入文件的类,因此我们将使用Java IO类将Scala写入文件。 下面是一个简单的程序,显示了如何在Scala中写入文件。

Write.scala

Write.scala

import java.io.File
import java.io.PrintWriterimport scala.io.Sourceobject Write {def main(args: Array[String]) {val writer = new PrintWriter(new File("Write.txt"))writer.write("Hello Developer, Welcome to Scala Programming.")writer.close()Source.fromFile("Write.txt").foreach { x => print(x) }}}

This will produce a file Write.txt with given content and then Source will read and print the same file data.

这将产生具有给定内容的文件Write.txt,然后Source将读取并打印相同的文件数据。

That’s all for Scala File IO example. You can see how simple it is to read file in Scala and write file in Scala using Java IO classes.

这就是Scala File IO示例的全部内容。 您可以看到使用Java IO类在Scala中读取文件和在Scala中写入文件有多么简单。

翻译自: https://www.journaldev.com/8278/scala-file-io-write-file-read-file

scala 写入文件

scala 写入文件_Scala文件IO –写入文件,读取文件相关推荐

  1. 利用IO流一次性读取文件中的所有内容,利用IO流下载文件

    利用IO流一次性读取文件中的所有内容 读取文件效率最快的方法就是一次全读进来,使用readline()之类的方法,可能需要反复访问文件,而且每次readline()都会调用编码转换,降低了速度,所以, ...

  2. 文件------概念、基本操作、打开文件的方式、按行读取文件内容、复制文件、文件/目录的常用管理操作、文本文件的编码格式、练习1-4

    文件的概念 文件的概念 计算机的文件,就是存储在某种长期储存设备上的一段数据 长期存储设备包括:硬盘.U盘.移动硬盘.光盘... 文件的作用 将数据长期保存下来,在需要的时候使用 文件的存储方式 在计 ...

  3. java读文件几种方式_java中读取文件的方式有哪几种

    java中读取文件的方式有哪几种 发布时间:2020-06-19 13:36:48 来源:亿速云 阅读:135 作者:鸽子 读取文件有多种方式,基于传统的输入流方式或基于nio的Buffer缓冲对象和 ...

  4. MATLAB如何读取文件某一行的内容,如何读取文件的某一行

    C语言 怎么用fgets()读取一个txt文件中的任意行的信息 C语言 怎么用fgets()读取一个txt文件中的任意行的信息 比如 szhfias sdf如果使用fgets()读取某个文件,第一次读 ...

  5. 02功能之读写文件流操作(C语言实现读取文件指定一行)

    02功能之读写文件流操作(C语言实现读取文件指定一行) 1 C语言读取文件指定一行 // 读取文件指定一行 int ReadLine1(const char *fileName, char outBu ...

  6. c语言lua读文件,file-io – 在Lua中逐行读取文件

    Lua使用相同的底层C实现模型管理文件(此模型也被其他编程语言使用,这是相当常见的).如果您不熟悉这种查看文件的方式,那么术语可能不清楚. 在该模型中,文件表示为具有所谓的当前位置的字节流.当前位置是 ...

  7. mysql 读取文件_关于mysql:逐行读取文件而不将整个文件加载到内存中

    我正在使用50 Gb MySQL导出文件,并对其执行脚本操作列表以转换为SQLite3可加载形式(我从这里得到的线索:脚本将mysql dump sql文件转换为可以导入sqlite3的格式D b ) ...

  8. python读取数据文件夹_使用python依次读取文件中的所有csv格式的数据

    使用python依次读取文件中的所有csv格式的数据: #coding=gbk import pandas as pd import os path = r'D:\ml_datasets\PHM\c6 ...

  9. java 到服务器上读文件路径,java访问linux服务器读取文件路径

    java访问linux服务器读取文件路径 内容精选 换一换 通过ADC从Host获取文件.参见准备环境完成环境配置.以运行用户登录安装Toolkit组件的服务器.执行命令,从Host获取B.java, ...

  10. python panda读取csv_python pandas 中文件的读写——read_csv()读取文件

    read_csv()读取文件 1.python读取文件的几种方式 read_csv 从文件,url,文件型对象中加载带分隔符的数据.默认分隔符为逗号 read_table 从文件,url,文件型对象中 ...

最新文章

  1. python开发pc软件_程序员带你十天快速入门Python,玩转电脑软件开发(二)
  2. 【ARM】Tiny4412裸板编程之异常
  3. 我来了,我看见了,我征服了
  4. Eigen库数据结构内存对齐问题
  5. 中国大学MOOC 视频字幕获取方法
  6. vmware 14 密钥
  7. mdx 医学词典_一些西医学方面词典的介绍
  8. 如何本地修改dns服务器,如何更新本地DNS?
  9. 520表白小程序设计Python代码详解(PyQt5界面,B站动漫风)
  10. Python获取英雄联盟的皮肤原画:新手玩家们都懵了!(一)
  11. 41、基于51单片机手机无线充电器系统锂电池存电系统设计
  12. 手把手教你用EZDML批量生成vue-element-admin前端页面代码
  13. element表格固定表头每列宽度,最右侧固定后,溢出出现滚动条内容和表头不能同时移动
  14. 用树莓派做MIDI HOST,给合成器外接MIDI键盘
  15. 上课第一天初感。。。
  16. 在R、Python和Julia中常用的数据可视化技术
  17. 拦截手机app发起的请求,获取请求信息!调试必备!!!
  18. MapReduce发生Permission denied: user=zhen, access=WRITE错误
  19. h5跳转到小程序总是出现页面不存在-微信小程序开发
  20. Python黑马头条推荐系统第四天 TensorFlow框架介绍和深度学习

热门文章

  1. 转载:网关的概念以及形象的比喻
  2. [转载] python字符串_一文详解Python字符串条件判断方法
  3. verilog之编程应该注意的事项
  4. 末学者笔记--Jenkins+Git+Gitlab+Ansible实现持续集成自动化部署静态网站
  5. Choerodon 的微服务之路(二):Choerodon 的微服务网关
  6. PLL与PHY的连接:通道绑定或者不绑定
  7. C#使用NPOI导出excel设置单元格背景颜色
  8. 判断字符串中是否包含指定字符的N种方法对比
  9. PBOC规范研究之六、变长记录文件(转)
  10. 如何在Azure上创建和部署云服务