本文整理汇总了Scala中scala.io.BufferedSource类的典型用法代码示例。如果您正苦于以下问题:Scala BufferedSource类的具体用法?Scala BufferedSource怎么用?Scala BufferedSource使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BufferedSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ConfigLoader
//设置package包名称以及导入依赖的类
package co.teapot.tempest.util
import java.io.File
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
import scala.io.BufferedSource
import scala.reflect.ClassTag
object ConfigLoader {
def loadConfig[T:ClassTag](source: BufferedSource): T = {
val yaml = new Yaml(new Constructor(implicitly[ClassTag[T]].runtimeClass))
yaml.load(source.getLines().mkString("\n")).asInstanceOf[T]
}
def loadConfig[T:ClassTag](configFile: File): T = {
if (configFile.exists) {
loadConfig(scala.io.Source.fromFile(configFile))
} else {
throw new Exception("Configuration file not found: " + configFile.getCanonicalPath)
}
}
def loadConfig[T:ClassTag](configFileName: String): T = {
loadConfig(new File(configFileName))
}
}
示例2: DataSource
//设置package包名称以及导入依赖的类
package io.github.mijicd.prezi.modules
import com.fasterxml.jackson.databind.ObjectMapper
import com.google.inject.{Provides, Singleton}
import com.twitter.inject.TwitterModule
import io.github.mijicd.prezi.domain.Presentation
import scala.collection.JavaConverters._
import scala.io.BufferedSource
object DataSource extends TwitterModule with Managed with DateConversion {
@Singleton
@Provides
def provideSource: Seq[Presentation] = {
val resource = getClass.getResource("/prezis.json")
using(scala.io.Source.fromURL(resource)) { source =>
val items = loadFrom(source)
sortSeq(items)
}
}
private def loadFrom(source: BufferedSource): Seq[Presentation] = {
val json = source.getLines().mkString
val mapper = new ObjectMapper()
val factory = mapper.getTypeFactory
val items: java.util.List[Presentation] =
mapper.readValue(json,
factory.constructCollectionType(classOf[java.util.List[Presentation]],
classOf[Presentation]))
items.asScala
}
private def sortSeq(seq: Seq[Presentation]) = seq.sortWith(compare)
private def compare(first: Presentation, second: Presentation): Boolean = {
val firstDate = dateOf(first.createdAt)
val secondDate = dateOf(second.createdAt)
firstDate.isAfter(secondDate)
}
}
示例3: FilterChecker
//设置package包名称以及导入依赖的类
package fileSearcher
import java.io.File
import scala.io.{Source, BufferedSource}
import scala.util.{Try, Success, Failure}
class FilterChecker(filter: String) {
val filterAsRegex = filter.r
def matchesFileContentCount(file: File): Int = {
def getFilterMatchCount(content: String): Int =
(filterAsRegex findAllIn content).length
def fileContentOccurances(fs: BufferedSource): Int =
fs.getLines().foldLeft(0)(
(accumulator, line) => accumulator + getFilterMatchCount(line)
)
val fileSource = Try(Source.fromFile(file))
fileSource match {
case Success(fs) => {
Try(fileContentOccurances(fs)) match {
case Success(n) => n
case Failure(_) => 0
}
}
case Failure(_) => 0
}
}
def matches(content: String): Boolean =
filterAsRegex findFirstMatchIn content match {
case Some(_) => true
case None => false
}
def findMatchedFiles(iOObjects: List[IOObject]): List[IOObject] =
for(iOObject <- iOObjects
if iOObject.isInstanceOf[FileObject]
if matches(iOObject.name))
yield iOObject
}
object FilterChecker {
def apply(filter: String): FilterChecker = new FilterChecker(filter)
}
示例4: E46
//设置package包名称以及导入依赖的类
package fi.kajstrom.efpscala.E46
import fi.kajstrom.efpscala.Util.FilePath
import scala.collection.mutable
import scala.io.{BufferedSource, Source}
object E46 extends App{
val file = Source.fromFile(FilePath.makeResourcePath("E46_in.txt"))
val words: List[String] = splitToWords(file)
val wordCounts : mutable.Map[String, Int] = mutable.Map()
words.foreach((word: String) => wordCounts(word) = wordCounts.getOrElse(word, 0) + 1)
wordCounts.foreach((word: (String, Int)) => {
print(word._1 + ": ")
println("".padTo(word._2, "*").mkString)
})
private def splitToWords(file: BufferedSource): List[String] = {
var words: List[String] = List()
for (line <- file.getLines()) {
line.mkString.split(" ").foreach(words :+= _)
}
words
}
}
示例5: ParseFromFile
//设置package包名称以及导入依赖的类
package com.github.raduba.gis
import scala.io.BufferedSource
import scala.util.control.NonFatal
import scala.util.{Failure, Success, Try}
object ParseFromFile {
def main(args: Array[String]) = {
if (args.isEmpty) println("Usage: parse <fileName>")
else {
val fileName = args.head
println(s"Parsing file $fileName")
val parseResult = parse(fileName)
parseResult match {
case Success(p) => println(s"Successfully parsed $p of type ${p.getClass.getName}")
case Failure(e) => println(s"Error on parsing geometry: ${e.getMessage}")
}
}
}
def parse(fileName: String): Try[Geometry] = {
var source: BufferedSource = null
try {
source = scala.io.Source.fromFile(fileName)
val wkt = source.mkString
val parseResult = WKTParser.parse(WKTParser.geometry, wkt)
parseResult match {
case WKTParser.Success(p, _) => Success(p)
case WKTParser.Error(msg, _) => Failure(new Exception(msg))
case WKTParser.Failure(msg, _) => Failure(new Exception(msg))
}
} catch {
case NonFatal(e) => Failure(e)
} finally {
if (source != null) Try(source.close())
}
}
}
示例6: WikipediaXMLDumpParser
//设置package包名称以及导入依赖的类
package it.agilelab.bigdata.spark.search.utils
import java.io.InputStream
import scala.io.BufferedSource
import scala.xml.pull._
class WikipediaXMLDumpParser(xmlInputStream: InputStream) extends Iterable[(String, String)] {
def iterator = new Iterator[(String, String)] {
val xml = new XMLEventReader(new BufferedSource(xmlInputStream, 524288))
def hasNext = xml.hasNext //TODO: proper check! currently it returns an extra empty page as the last one.
def next = {
var inPage = false
var inTitle = false
var inText = false
var pageReady = false
var title, text = ""
while (xml.hasNext && !pageReady) {
var event = xml.next()
event match {
case event: EvElemStart => {
val castedEvent = event.asInstanceOf[EvElemStart]
if (castedEvent.label.equalsIgnoreCase("page")) {
inPage = true
} else if (inPage && castedEvent.label.equalsIgnoreCase("title")) {
inTitle = true
} else if (inPage && castedEvent.label.equalsIgnoreCase("text")) {
inText = true
}
}
case event: EvText => {
val castedEvent = event.asInstanceOf[EvText]
if (inTitle) {
title = castedEvent.text
} else if (inText) {
text = if (text == "") castedEvent.text else text + "\"" + castedEvent.text
}
}
case event: EvElemEnd => {
val castedEvent = event.asInstanceOf[EvElemEnd]
if (castedEvent.label.equalsIgnoreCase("page")) {
inPage = false
pageReady = true
} else if (inPage && castedEvent.label.equalsIgnoreCase("title")) {
inTitle = false
} else if (inPage && castedEvent.label.equalsIgnoreCase("text")) {
inText = false
}
}
case _ => ;
}
}
(title, text)
}
}
}
示例7: TrecTopicParser
//设置package包名称以及导入依赖的类
package it.agilelab.bigdata.spark.search.utils
import java.io.InputStream
import scala.io.BufferedSource
import scala.xml.pull.{EvElemEnd, EvElemStart, EvText, XMLEventReader}
class TrecTopicParser(xmlInputStream: InputStream) extends Iterable[(Int, String)] {
def iterator = new Iterator[(Int, String)] {
val xml = new XMLEventReader(new BufferedSource(xmlInputStream, 524288))
def hasNext = xml.hasNext //TODO: proper check! currently it returns an extra empty page as the last one.
def next = {
var inTopic = false
var inQuery = false
var topicReady = false
var topicId = 0
var query = ""
while (xml.hasNext && !topicReady) {
var event = xml.next()
event match {
case event: EvElemStart => {
val castedEvent = event.asInstanceOf[EvElemStart]
if (castedEvent.label.equalsIgnoreCase("topic")) {
inTopic = true
topicId = event.attrs("number").head.text.toInt
} else if (inTopic && castedEvent.label.equalsIgnoreCase("query")) {
inQuery = true
}
}
case event: EvText => {
val castedEvent = event.asInstanceOf[EvText]
if (inQuery) {
query = castedEvent.text
}
}
case event: EvElemEnd => {
val castedEvent = event.asInstanceOf[EvElemEnd]
if (castedEvent.label.equalsIgnoreCase("topic")) {
inTopic = false
topicReady = true
} else if (inTopic && castedEvent.label.equalsIgnoreCase("query")) {
inQuery = false
}
}
case _ => ;
}
}
(topicId, query)
}
}
}
示例8: WetPathLoader
//设置package包名称以及导入依赖的类
package com.jeffharwell.commoncrawl.wetpathloader
import java.io.File
import java.sql.DriverManager
import java.sql.Connection
import scala.io.BufferedSource
class WetPathLoader(driver: String, url: String, username: String, password: String) {
Class.forName(driver)
val connection = DriverManager.getConnection(url, username, password)
val statement = connection.createStatement()
def loadPaths(wetfile: BufferedSource) = {
var counter = 0
var batchsize = 1000
// Prepare the sql statement
val sql = "insert into wetpaths (path) values (?)"
val ps = connection.prepareStatement(sql)
print("Working:")
wetfile.getLines().foreach { l =>
counter = counter + 1
ps.setString(1, l)
ps.addBatch()
if (counter % batchsize == 0) {
print('.')
ps.executeBatch()
}
}
// Catch any stragglers
if ( counter % batchsize != 0) {
ps.executeBatch()
}
ps.close()
connection.close()
println("\nDone");
}
}
示例9: Mnemonics
//设置package包名称以及导入依赖的类
package c1.w6
import scala.io.{BufferedSource, Source}
class Mnemonics {
val in: BufferedSource = Source.fromResource("linuxwords.txt")
val words: Stream[String] = in.getLines.toStream filter (word => word forall (chr => chr.isLetter))
// Mnemonic map
val mnemonicMap = Map(
'2' -> "ABC", '3' -> "DEF", '4' -> "GHI", '5' -> "JKL",
'6' -> "MNO", '7' -> "PQRS", '8' -> "TUV", '9' -> "WXYZ"
)
var char2Code: Map[Char, Char] = {
for {
(digit, str) <- mnemonicMap
abc <- str
} yield abc -> digit
}
def word2Code(word: String): String = word.toUpperCase map char2Code
val wordsForNum: Map[String, Seq[String]] = {
words groupBy word2Code withDefaultValue Seq()
}
def encode(number: String): Set[List[String]] = {
if (number.isEmpty) Set(List())
else {
for {
split <- 1 to number.length
word <- wordsForNum(number take split)
rest <- encode(number drop split)
} yield word :: rest
}.toSet
}
def translate(number: String): Set[String] = {
encode(number) map (_ mkString " ")
}
}
示例10: meanings
//设置package包名称以及导入依赖的类
package com.hyenawarrior.dictionaryLoader
import scala.io.BufferedSource
Set(), fieldsOfRecord.tail.map(parseWordDef).toList)
}
def meanings(language: String): List[MeaningDefinition] =
{
val src = getEntries(language)
try
{
src.getLines.map(line =>
{
val Array(meaningIdStr, trunk) = line split ':'
val meaningId = meaningIdStr.toInt
val parts = trunk split ']' map ( _ drop 1 )
val (meaningDescSet, wordDescList) = extractDescFromRecord(parts)
MeaningDefinition(meaningId, meaningDescSet, wordDescList)
})
.toList
}
finally
{
src.close
}
}
protected def getEntries(language: String): BufferedSource
}
示例11: SocketConnection
//设置package包名称以及导入依赖的类
package servicebus
import java.io.InputStreamReader
import java.net.{InetAddress, Socket}
import scala.io.BufferedSource
object SocketConnection {
def connect(ip: String) {
println("hey")
val socket: Socket = new Socket(InetAddress.getByName(ip), 11000)
println("wtf")
val source: BufferedSource = new BufferedSource(socket.getInputStream)
while (true) {
val line: String = source.bufferedReader().readLine()
println(line)
}
}
def main(args: Array[String]): Unit = {
SocketConnection.connect("51.4.231.251")
}
}
示例12: PrizeTote
//设置package包名称以及导入依赖的类
package com.brianbickerton
import com.typesafe.scalalogging.StrictLogging
import scala.io.BufferedSource
object PrizeTote extends StrictLogging {
def main(args: Array[String]): Unit = {
Config.parser.parse(args, Config()) match {
case Some(config) =>
val toteDimensions = Seq(
config.toteLength,
config.toteWidth,
config.toteHeight).sorted
val maxVolume = toteDimensions.product
val dimensionList = toteDimensions.map(d => s"${d}cm").mkString(" x ")
logger.info(s"Tote: $dimensionList, ${maxVolume}cm^3")
var source: Option[BufferedSource] = None
val allProducts = try {
source = Some(io.Source.fromFile(config.packagesFile))
source.get.getLines().map(Product.fromCSVLine).toList
} finally {
if (source.isDefined) source.get.close()
}
val products = allProducts.filter(
p =>
!p.dimensions.zipWithIndex.
exists(d => d._1 > toteDimensions(d._2))
)
logger.info(s"Total products in csv: ${allProducts.size}")
logger.info(s"Products able to fit in the tote: ${products.size}")
val optimalTote = Tote.findOptimalTote(products, maxVolume)
logger.info(s"=== OPTIMAL TOTE ===")
logger.info(optimalTote.toString)
logger.info(s"=== EMAIL ===")
logger.info(s"${optimalTote.items.sum}@redmart.com")
case None => System.exit(1)
}
}
}
示例13: read
//设置package包名称以及导入依赖的类
import scala.io.{Source, BufferedSource}
trait Reader{
type In <: java.io.Serializable
type Content
def read(in : In) : Content
}
class FileReader extends Reader{
type In =String
type Content = BufferedSource
override def read(name :In) =Source.fromFile(name)
}
//???????????????????,??
//??????????,???List[T] ,???????????List[String]
//??????????????,???????????(?Reader)
//????????????,??????????
//??????????????????????
object Abstract_Type {
def main(args: Array[String]) {
val filereader = new FileReader
val content = filereader.read("E:abc.txt")
for(line <- content.getLines){
println(line)
}
}
}
示例14: LoadPublicHolidayList
//设置package包名称以及导入依赖的类
package datadefinitions.tfl.loadresources
import java.text.SimpleDateFormat
import java.util.{Date, Locale}
import datadefinitions.LoadResourceFromSource
import play.api.Logger
import scala.io.BufferedSource
object LoadPublicHolidayList extends LoadResourceFromSource {
override val bufferedSource: BufferedSource = DEFAULT_PUBLIC_HOLIDAY_LIST_FILE
lazy val publicHolidayList:List[Date] = {
var publicHolidayList:List[Date] = List()
bufferedSource.getLines().drop(1).foreach((line) => {
try {
val sdf = new SimpleDateFormat("dd/M/yyyy", Locale.UK)
val date = sdf.parse(line)
publicHolidayList = publicHolidayList :+ date
}
catch {
case e: Exception =>
Logger.error("Error reading public holiday list file. Error on line: " + line)
throw new Exception("Error reading public holiday list file. Error on line: " + line)
}
})
Logger.info("Public holiday List Loaded")
publicHolidayList
}
}
示例15: LoadStopIgnoreList
//设置package包名称以及导入依赖的类
package datadefinitions.tfl.loadresources
import datadefinitions.LoadResourceFromSource
import play.api.Logger
import scala.io.BufferedSource
object LoadStopIgnoreList extends LoadResourceFromSource {
override val bufferedSource: BufferedSource = DEFAULT_STOP_IGNORE_LIST_FILE
lazy val stopIgnoreSet:Set[String] = {
var stopIgnoreSet:Set[String] = Set()
bufferedSource.getLines().drop(1).foreach((line) => {
//drop first row and iterate through others
try {
val splitLine = line.split(",")
stopIgnoreSet += splitLine(0)
}
catch {
case e: Exception =>
Logger.error("Error reading stop ignore listfile. Error on line: " + line)
throw new Exception("Error reading stop ignore listfile. Error on line: " + line)
}
})
Logger.info("Stop Ignore List Loaded")
stopIgnoreSet
}
}