ReadChars and WriteChars are traits that contain the character and string methods of Input and Output. The primary difference is that the Charset is defined by the underlying resource rather than supplied at the method invocation site.
Compare two methods:
Input:
1 | def chars( implicit codec : Codec = Codec.default) : LongTraversable[Char] |
1 | def chars : LongTraversable[Char] |
Not many examples are needed to explain these concepts but here are a few examples on how to create ReadChar and WriteChar objects:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import scalax.io. _ import JavaConverters. _ // JavaConverters has asReadChars and asWriteChars // for converting some objects to ReadChars and WriteChars // (The JavaConverters post will explain // more about the JavaConverters object) "Hello World" .asReadChars.chars.size val writer = new java.io.StringWriter() // Resource object can be used to create a ReadChars and WriteChars val writeChars = Resource.fromWriter(writer).write( "Yeee Hawww!" ) println(Resource.fromReader( new java.io.StringReader(writer.toString)).lines().size) // ReadChars and WriteChars can be obtained from // InputResource and OutputResource object respectively // SeekableByteChannelResource[SeekableByteChannel] // (returned by fromFile) implements both traits and // therefore has both methods val fileResource = Resource.fromFile( "somefile.txt" ) implicit val codec = Codec.UTF 8 // clear any old data in file fileResource.truncate( 0 ) // Codec used is declared when calling writer or reader // methods fileResource.writer.write( "hi" ) println(fileResource.reader.chars.size) |
Good methods to understand it, for more information and training , free tutorials, demo can visit.....
ReplyDeleteCheck this site Tekslate for in scala Training
Go here if you’re looking for information scala Training