One of the hurdles many learners of JVM languages face is: how do I read a goddamn file? And then you have your face blasted with DataInputStreams, FileReaders, buffered this, buffered that, channels or whatever. This is for Scala programmers of all levels and is concerned with reading text files.
Assume you have a file path and you want to read some text data from it:. The plain old Scanner will do. A Scanner is a stateful object that allows reading characters or lines directly:. Over the same text file Demo3. This can be done using the slice function that takes the range from and until. In the above example if we want to operate over two lines of a file we can use the.
We can also change the file to List or to array after reading it by using the method. Disposing or Closing of a file is also needed as it takes up the memory over the JVM. And even for automatically closing we can use the. So this buffered source has to be closed once the operations are done over them. So we use the.
Here from the above article we saw how we can use the various method to read file in Scala. We also saw how the Scala. Source provides method to read files in scala and perform operation over them.
With the help of various examples, we saw the different aspects and the advantages of using reading files using different method. This is a guide to Scala Read File. Control Statements. OOP Concepts. Scala Packages.
Scala Trait. Table of Contents. Improve Article. Save Article. Like Article. Last Updated : 17 Mar, Previous Chained Package Clauses in Scala. Next Scala Traits. Recommended Articles. Scala SortedMap addString method with a start, a separator and an end with example.
0コメント