Scala Cookbook by Alvin Alexander
Author:Alvin Alexander [Alvin Alexander]
Language: eng
Format: epub, mobi, pdf
Tags: COMPUTERS / Programming Languages / Java
ISBN: 9781449340315
Publisher: O’Reilly Media
Published: 2013-07-31T16:00:00+00:00
12.1. How to Open and Read a Text File
Problem
You want to open a plain-text file in Scala and process the lines in that file.
Solution
There are two primary ways to open and read a text file:
Use a concise, one-line syntax. This has the side effect of leaving the file open, but can be useful in short-lived programs, like shell scripts.
Use a slightly longer approach that properly closes the file.
This solution shows both approaches.
Using the concise syntax
In Scala shell scripts, where the JVM is started and stopped in a relatively short period of time, it may not matter that the file is closed, so you can use the Scala scala.io.Source.fromFile method as shown in the following examples.
To handle each line in the file as it’s read, use this approach:
import scala.io.Source val filename = "fileopen.scala" for (line <- Source.fromFile(filename).getLines) { println(line) }
As a variation of this, use the following approach to get all of the lines from the file as a List or Array:
val lines = Source.fromFile("/Users/Al/.bash_profile").getLines.toList val lines = Source.fromFile("/Users/Al/.bash_profile").getLines.toArray
The fromFile method returns a BufferedSource, and its getLines method treats “any of \r\n, \r, or \n as a line separator (longest match),” so each element in the sequence is a line from the file.
Use this approach to get all of the lines from the file as one String:
val fileContents = Source.fromFile(filename).getLines.mkString
This approach has the side effect of leaving the file open as long as the JVM is running, but for short-lived shell scripts, this shouldn’t be an issue; the file is closed when the JVM shuts down.
Download
Scala Cookbook by Alvin Alexander.mobi
Scala Cookbook by Alvin Alexander.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12568)
Hello! Python by Anthony Briggs(9913)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9795)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8295)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7056)
Microservices with Go by Alexander Shuiskov(6819)
Practical Design Patterns for Java Developers by Miroslav Wengner(6736)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6677)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6083)
The Art of Crafting User Stories by The Art of Crafting User Stories(5609)
NetSuite for Consultants - Second Edition by Peter Ries(5549)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5349)
Kotlin in Action by Dmitry Jemerov(5062)
