Édition 2019 (8 ans plus tard), Scala-IO n'étant pas très actif, le cas échéant, Li Haoyi propose sa propre bibliothèque lihaoyi/os-lib
qu'il présente ci-dessous .
Juin 2019, Xavier Guihot mentions dans sa réponse la bibliothèque Using
un utilitaire pour effectuer une gestion automatique des ressources.
Edit (septembre 2011) : depuis Eduardo Costa pose des questions sur Scala2.9, et puisque Rick-777 commentaires que scalax.IO historique des livraisons est à peu près inexistant depuis la mi-2009...
Scala-IO a changé de place : voir son Repo GitHub , de Jesse Eichar (également sur le SO ) :
Le projet Scala IO est composé de plusieurs sous-projets portant sur différents aspects et extensions de l'IO.
Il existe deux composants principaux de Scala IO :
-
Core - Core traite principalement de la lecture et de l'écriture de données vers et depuis des sources et des puits arbitraires. Les traits de base sont
Input
, Output
y Seekable
qui fournissent l'API de base.
Les autres classes d'importance sont Resource
, ReadChars
y WriteChars
.
-
Fichier - Le fichier est un
File
(appelé Path
) qui est basée sur une combinaison du système de fichiers Java 7 NIO et des API SBT PathFinder.
Path
y FileSystem
sont les principaux points d'entrée dans l'API Scala IO File.
import scalax.io._
val output:Output = Resource.fromFile("someFile")
// Note: each write will open a new connection to file and
// each write is executed at the begining of the file,
// so in this case the last write will be the contents of the file.
// See Seekable for append and patching files
// Also See openOutput for performing several writes with a single connection
output.writeIntsAsBytes(1,2,3)
output.write("hello")(Codec.UTF8)
output.writeStrings(List("hello","world")," ")(Codec.UTF8)
Réponse originale (janvier 2011), avec l'ancien emplacement de scala-io :
Si vous ne voulez pas attendre Scala2.9, vous pouvez utiliser la fonction scala-incubateur / scala-io bibliothèque.
(comme mentionné dans " Pourquoi Scala Source ne ferme-t-il pas l'InputStream sous-jacent ? ")
Ver les échantillons
{ // several examples of writing data
import scalax.io.{
FileOps, Path, Codec, OpenOption}
// the codec must be defined either as a parameter of ops methods or as an implicit
implicit val codec = scalax.io.Codec.UTF8
val file: FileOps = Path ("file")
// write bytes
// By default the file write will replace
// an existing file with the new data
file.write (Array (1,2,3) map ( _.toByte))
// another option for write is openOptions which allows the caller
// to specify in detail how the write should take place
// the openOptions parameter takes a collections of OpenOptions objects
// which are filesystem specific in general but the standard options
// are defined in the OpenOption object
// in addition to the definition common collections are also defined
// WriteAppend for example is a List(Create, Append, Write)
file.write (List (1,2,3) map (_.toByte))
// write a string to the file
file.write("Hello my dear file")
// with all options (these are the default options explicitely declared)
file.write("Hello my dear file")(codec = Codec.UTF8)
// Convert several strings to the file
// same options apply as for write
file.writeStrings( "It costs" :: "one" :: "dollar" :: Nil)
// Now all options
file.writeStrings("It costs" :: "one" :: "dollar" :: Nil,
separator="||\n||")(codec = Codec.UTF8)
}
1 votes
Si vous savez comment faire en Java, vous pouvez faire de même en Scala. Votre question concerne-t-elle spécifiquement la bibliothèque standard de Scala ?
1 votes
@wheaties oui la meilleure façon de faire cela en scala
0 votes
Cette bibliothèque est vraiment bien : github.com/pathikrit/better-files
1 votes
La bibliothèque OS-Lib de Lihaoyi github.com/lihaoyi/os-lib