Freitag, 9. Oktober 2009

read file in Scala

read file in scala:

for (file <- new File("/var/log/").listFiles) {


list a directory

if (!file.isDirectory) {

ask is file not a directory

simple print out of a file

import scala.io.Source

for {
(line) <- Source.fromFile("bla.txt").getLines
} print(line)


read a file in an iterator

scala> val lines = scala.io.Source.fromFile("/var/log/syslog")
lines: scala.io.Source = non-empty iterator

scala> lines
res5: scala.io.Source = non-empty iterator


read a file in a string

val lines = scala.io.Source.fromFile("/var/log/syslog").mkString

// for utf-8 encoding
val lines = scala.io.Source.fromFile("file.txt", "utf-8").getLines.mkString



or you can use Java API

val os = new BufferedOutputStream(new FileOutputStream("gen/" + file.getName))

1 Kommentar:

  1. Java Platform, Enterprise Edition (Java EE) is the industry-standard platform for building enterprise-class applications coded in the Java programming language by software development company. The most efficient open source products are used to save the Java software development time and cost keeping up the high quality work. Custom software development is a type of software that is developed either for a specific company or business entity. Small business or large enterprise, our java software development company offers proficiency and capability in all aspects of your Java custom software development project. Java enterprise application development services designed to help businesses. Java Enterprise software development succeed because stable Java standards help developers create multilevel applications with a component based approach. As a Java Enterprise web development outsourcing partner Tenax Technologies assures latest technologies using Java.

    AntwortenLöschen