patternMinor
Simple wrapper around Java's File & FileInputStream
Viewed 0 times
aroundfilesimplejavawrapperfileinputstream
Problem
In an attempt to begin learning Scala, I've taken a stab at creating a simple wrapper around Java's File & FileInputStream in order to read lines from a file. This functionality already exists in the Source class and is just a learning exercise.
If you were building this wrapper in Scala, is there anything you might change? Are there any best practices I'm overlooking?
```
////////// Driver.scala ////////////////
object Driver {
def main(args: Array[String]): Unit = {
val uri = File.classpathResource("sample.txt")
val fileReader = FileReader.open(uri)
println("Reading File: " + fileReader.file.name() )
for( val line JavaFile}
import java.net.URI
/** This object provides convenience methods for working with local file resources */
object File {
/** Helper method to resolve a URI for a resource on the classpath
*
* @param the resource's path name
* @return the resource's URI
*/
def classpathResource(pathName: String): URI = {
val url = this.getClass().getClassLoader().getResource(pathName)
url.toURI()
}
}
/** This class represents a file on the file system */
class File(val javaFile: JavaFile) {
def this(pathName: String) = this(new JavaFile(pathName))
def this(uri: URI) = this(new JavaFile(uri))
/** Get the file's file name */
def name(): String = javaFile.getName()
/** Get the file's absolute path */
def path(): String = javaFile.getAbsolutePath()
/** Open a new file reader. Call FileReader.close() once you have finished
* using the file reader.
*
* @return a new file reader instance
* @throws FileNotFoundException if the file cannot be opened for reading
*/
def openReader(): FileReader = {
new FileReader(this)
}
}
////////// FileReader.scala ////////////////
import java.io.FileInputStream
import java.net.URI
/** This object provides convenience methods for reading files */
object FileReader {
/** Creates and opens a FileReader class instance for a given file
If you were building this wrapper in Scala, is there anything you might change? Are there any best practices I'm overlooking?
```
////////// Driver.scala ////////////////
object Driver {
def main(args: Array[String]): Unit = {
val uri = File.classpathResource("sample.txt")
val fileReader = FileReader.open(uri)
println("Reading File: " + fileReader.file.name() )
for( val line JavaFile}
import java.net.URI
/** This object provides convenience methods for working with local file resources */
object File {
/** Helper method to resolve a URI for a resource on the classpath
*
* @param the resource's path name
* @return the resource's URI
*/
def classpathResource(pathName: String): URI = {
val url = this.getClass().getClassLoader().getResource(pathName)
url.toURI()
}
}
/** This class represents a file on the file system */
class File(val javaFile: JavaFile) {
def this(pathName: String) = this(new JavaFile(pathName))
def this(uri: URI) = this(new JavaFile(uri))
/** Get the file's file name */
def name(): String = javaFile.getName()
/** Get the file's absolute path */
def path(): String = javaFile.getAbsolutePath()
/** Open a new file reader. Call FileReader.close() once you have finished
* using the file reader.
*
* @return a new file reader instance
* @throws FileNotFoundException if the file cannot be opened for reading
*/
def openReader(): FileReader = {
new FileReader(this)
}
}
////////// FileReader.scala ////////////////
import java.io.FileInputStream
import java.net.URI
/** This object provides convenience methods for reading files */
object FileReader {
/** Creates and opens a FileReader class instance for a given file
Solution
Looks ok. A
The first two being
Traversable is a more interesting way to do this, since you can then auto-close the resource. There's one thing I took an issue to:private val inputStream = new FileInputStream(file.javaFile)
private val lineIterator = new BufferedLineIterator(inputStream);
def lines(): Iterator[String] = lineIteratorThe first two being
val means this cannot be reused. However, if you turn them into def, then lines will already return a valid iterator.Code Snippets
private val inputStream = new FileInputStream(file.javaFile)
private val lineIterator = new BufferedLineIterator(inputStream);
def lines(): Iterator[String] = lineIteratorContext
StackExchange Code Review Q#15457, answer score: 2
Revisions (0)
No revisions yet.