FileReader( "hoge.txt" ).reader.forEachLine{
println( it )
}
val lines = FileReader( "hoge.txt" ).readLines()
for( str in lines ){ println( str ) }
シーケンスを生成してブロックが実行される
シーケンスなので、1行ずつ読み込みながらブロックが実行される
val list = File( "hoge.txt" ).reader().useLines{ it.filter(String::isNotBlank).toList() }
val list = File( "hoge.txt" ).reader().useLines(){ it.filter{str -> str.length > 0}.toList() }
val list = File( "hoge.txt" ).reader().useLines(){ it.take(3).toList() }
File( "hoge.txt" ).reader().useLines(){ it.forEach{ println( it ) } }
※バイナリファイルを Reader の useLines で読み込むと例外(MalformedInputException)がスローされるが、File の useLines で読み込むと例外はスローされない。
val text = FileReader( "hoge.txt" ).readText()
URL( "http://www.yahoo.co.jp/" ).openStream().bufferedReader().lineSequence().forEach{ println( it ) }
println( URL( "http://www.yahoo.co.jp/" ).readText())
val bytes = URL( "http://www.yahoo.co.jp/" ).readBytes()
val text = javaClass.getResourceAsStream( "/hoge.txt" ).use() { itreader().readText() }
File( "hoge.txt" ).reader().use() {
val sb = StringBuilder()
val buf = CharArray(64)
var len = -1
while( run { len = it.read( buf, 0, 64 ); len >= 0 } ) {
sb.append( buf, 0, len )
}
println( sb.toString())
}
val html = URL("http://host/").openStream().reader().use() {
return@use it.readText()
}
println(html)
File( "hoge.txt" ).bufferedReader().use() { reader ->
var str = reader.readLine()
while( str != null ) {
println( str )
str = reader.readLine()
}
}
File( "hoge.txt" ).writer().use() { it.write( "hoge\n" ) }
File( "hoge.txt" ).bufferedWriter().use() { it.write( "hoge" ); it.newLine() }
File( "hoge.txt" ).printWriter().use(){ it.println( "hoge" ) }
println( File( "hoge.txt" ).readText())
File( "hoge.txt" ).writeText( "hoge\n" )
File( "hoge.txt" ).appendText( "Hoge!\n" )
File( "/var/log/hoge.log" ).copyTo( File( "/tmp/hoge.txt" ), true )
File( "/tmp" ).copyRecursively( File( "/home/hoge" ), true )
File( "/tmp/hoge" ).deleteRecursively()
File("hoge.txt").extension // ==> "txt"
File( "hoge.txt" ).forEachBlock{ buf, size -> println( String( buf, 0, size )) }
File( "hoge.txt" ).forEachLine{ println( it ) }
File("/hoge/fuga.txt").isRooted // true
File("../fuga.txt").isRooted // false
File("/fuga/hoge.txt").nameWithoutExtension // "hoge"
File( "/tmp" ).walk().forEach{ println( it.absolutePath ) }
FileTreeWalk に対して maxDepth(1) を指定すると、直下(1階層)だけ探索するFile( "/tmp" ).walk().maxDepth(1).forEach{ println( it.absolutePath ) }
File( "/tmp" ).walk().filter{ it.extension == "txt" }.forEach{ println( it.absolutePath ) }
File( "/tmp" ).walk().sorted().forEach{ println( it ) }
File( "/tmp" ).walk().filter{ it.isFile() }.sortedBy{ it.name }.forEach{ println( it ) }
File( "/tmp" ).walk().filter{ it.isFile() }.sortedBy{ it.extension }.forEach{ println( it ) }
import java.util.*
File( "/tmp" ).walk().filter{ it.isFile() }.sortedWith( Comparator{ a, b -> a.extension.compareTo( b.extension ) } ).forEach{ println( it ) }
File( "/tmp" ).walk().groupBy{ it.extension }.forEach{ println( "${it.key}:" ); it.value.forEach{ println( " ${it.absolutePath}" )} }
(結果)
class:
hoge.class
hogee.class
fuga.class
fungaa.class
java:
hoge.java
fuga.java
kt:
hogee.kt
fungaa.kt
val tmpDir = createTempDir( "hoge" )
println( tmpDir.absoluteName )
tmpDir.delete()
val tmpFile = createTempFile( "hoge.txt" )
println( tmpFile.absoluteName )
tmpFile.delete()
if( System.`in`.available() > 0 ) {
System.`in`.bufferedReader().use {
do {
var str = it.readLine()
if( str.isNullOrEmpty()) continue
println( str )
} while( str != null )
}
}