0018.scala文件的读取写入操作代码_从数据库读取 scala代码-程序员宅基地

技术标签: scala  spark  

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.这里看到没有进行源码关联,下载源码,选择自己的版本
解压后,压缩成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)
  }
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/njiang/article/details/49975703

智能推荐

hdu 1229 还是A+B(水)-程序员宅基地

文章浏览阅读122次。还是A+BTime Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24568Accepted Submission(s): 11729Problem Description读入两个小于10000的正整数A和B,计算A+B。...

http客户端Feign——日志配置_feign 日志设置-程序员宅基地

文章浏览阅读419次。HEADERS:在BASIC的基础上,额外记录了请求和响应的头信息。FULL:记录所有请求和响应的明细,包括头信息、请求体、元数据。BASIC:仅记录请求的方法,URL以及响应状态码和执行时间。NONE:不记录任何日志信息,这是默认值。配置Feign日志有两种方式;方式二:java代码实现。注解中声明则代表某服务。方式一:配置文件方式。_feign 日志设置

[转载]将容器管理的持久性 Bean 用于面向服务的体系结构-程序员宅基地

文章浏览阅读155次。将容器管理的持久性 Bean 用于面向服务的体系结构本文将介绍如何使用 IBM WebSphere Process Server 对容器管理的持久性 (CMP) Bean的连接和持久性逻辑加以控制,使其可以存储在非关系数据库..._javax.ejb.objectnotfoundexception: no such entity!

基础java练习题(递归)_java 递归例题-程序员宅基地

文章浏览阅读1.5k次。基础java练习题一、递归实现跳台阶从第一级跳到第n级,有多少种跳法一次可跳一级,也可跳两级。还能跳三级import java.math.BigDecimal;import java.util.Scanner;public class Main{ public static void main(String[]args){ Scanner reader=new Scanner(System.in); while(reader.hasNext()){ _java 递归例题

面向对象程序设计(荣誉)实验一 String_对存储在string数组内的所有以字符‘a’开始并以字符‘e’结尾的单词做加密处理。-程序员宅基地

文章浏览阅读1.5k次,点赞6次,收藏6次。目录1.串应用- 计算一个串的最长的真前后缀题目描述输入输出样例输入样例输出题解2.字符串替换(string)题目描述输入输出样例输入样例输出题解3.可重叠子串 (Ver. I)题目描述输入输出样例输入样例输出题解4.字符串操作(string)题目描述输入输出样例输入样例输出题解1.串应用- 计算一个串的最长的真前后缀题目描述给定一个串,如ABCDAB,则ABCDAB的真前缀有:{ A, AB,ABC, ABCD, ABCDA }ABCDAB的真后缀有:{ B, AB,DAB, CDAB, BCDAB_对存储在string数组内的所有以字符‘a’开始并以字符‘e’结尾的单词做加密处理。

算法设计与问题求解/西安交通大学本科课程MOOC/C_算法设计与问题求解西安交通大学-程序员宅基地

文章浏览阅读68次。西安交通大学/算法设计与问题求解/树与二叉树/MOOC_算法设计与问题求解西安交通大学

随便推点

[Vue warn]: Computed property “totalPrice“ was assigned to but it has no setter._computed property "totalprice" was assigned to but-程序员宅基地

文章浏览阅读1.6k次。问题:在Vue项目中出现如下错误提示:[Vue warn]: Computed property "totalPrice" was assigned to but it has no setter. (found in <Anonymous>)代码:<input v-model="totalPrice"/>原因:v-model命令,因Vue 的双向数据绑定原理 , 会自动操作 totalPrice, 对其进行set 操作而 totalPrice 作为计..._computed property "totalprice" was assigned to but it has no setter.

basic1003-我要通过!13行搞定:也许是全网最奇葩解法_basic 1003 case 1-程序员宅基地

文章浏览阅读60次。十分暴力而简洁的解决方式:读取P和T的位置并自动生成唯一正确答案,将题给测点与之对比,不一样就给我爬!_basic 1003 case 1

服务器浏览war文件,详解将Web项目War包部署到Tomcat服务器基本步骤-程序员宅基地

文章浏览阅读422次。原标题:详解将Web项目War包部署到Tomcat服务器基本步骤详解将Web项目War包部署到Tomcat服务器基本步骤1 War包War包一般是在进行Web开发时,通常是一个网站Project下的所有源码的集合,里面包含前台HTML/CSS/JS的代码,也包含Java的代码。当开发人员在自己的开发机器上调试所有代码并通过后,为了交给测试人员测试和未来进行产品发布,都需要将开发人员的源码打包成Wa..._/opt/bosssoft/war/medical-web.war/web-inf/web.xml of module medical-web.war.

python组成三位无重复数字_python组合无重复三位数的实例-程序员宅基地

文章浏览阅读3k次,点赞3次,收藏13次。# -*- coding: utf-8 -*-# 简述:这里有四个数字,分别是:1、2、3、4#提问:能组成多少个互不相同且无重复数字的三位数?各是多少?def f(n):list=[]count=0for i in range(1,n+1):for j in range(1, n+1):for k in range(1, n+1):if i!=j and j!=k and i!=k:list.a..._python求从0到9任意组合成三位数数字不能重复并输出

ElementUl中的el-table怎样吧0和1改变为男和女_elementui table 性别-程序员宅基地

文章浏览阅读1k次,点赞3次,收藏2次。<el-table-column prop="studentSex" label="性别" :formatter="sex"></el-table-column>然后就在vue的methods中写方法就OK了methods: { sex(row,index){ if(row.studentSex == 1){ return '男'; }else{ return '女'; }..._elementui table 性别

java文件操作之移动文件到指定的目录_java中怎么将pro.txt移动到design_mode_code根目录下-程序员宅基地

文章浏览阅读1.1k次。java文件操作之移动文件到指定的目录_java中怎么将pro.txt移动到design_mode_code根目录下

推荐文章

热门文章

相关标签