Scala I/O Code Samples
Fall 2011, David Matuszek
/** Asks the user to choose a plain file.
*
* @param title What to put in the file chooser dialog title bar.
* @return The chosen file.
*/
def choosePlainFile(title: String = ""): Option[File] = {
val chooser = new FileChooser(new File("."))
chooser.title = title
val result = chooser.showOpenDialog(null)
if (result == FileChooser.Result.Approve) {
Some(chooser.selectedFile)
} else None
} |
/** Asks the user to choose a directory, then returns (as an option)
* an array of the files in that directory.
*
* @param title What to put in the file chooser dialog title bar.
* @return The files in the chosen directory.
*/
def getDirectoryListing(title: String = ""): Option[Array[File]] = {
val chooser = new FileChooser(null)
chooser.fileSelectionMode = FileChooser.SelectionMode.DirectoriesOnly
chooser.title = title
val result = chooser.showOpenDialog(null)
if (result == FileChooser.Result.Approve) {
Some(chooser.selectedFile.listFiles())
} else None
} |
/** Returns the words from a given text, where a word is defined
* as a nonempty sequence of letters only.
*
* @param text The input text.
* @return A list of words from the text.
*/
def getWordsFromText(text: String): List[String] = {
("""[a-zA-Z]+""".r findAllIn text) toList
}} |