admin管理员组文章数量:1397800
I'm working on a function to process some data and output the results as a csv file. I am using a 'use' block to wrap the function and ensure all the resources are closed. the problem I am running into is that I want the function to return the newly created File to the caller. The use block has the ability to return a value, however since it is being called with a bufferedWriter, there is no ability to actually return the File object from inside the block. Can anyone offer suggestions on how to solve this?
I want to do something like:
fun List<Contact>.buildCsv():File
{
return File("people.csv").bufferedWriter(Charsets.ISO_8859_1).use { out ->
out.write("a,b,c,d\n")
this.forEach {
out.write(it.toString())
}
}
}
but that code does not compile.
I'm working on a function to process some data and output the results as a csv file. I am using a 'use' block to wrap the function and ensure all the resources are closed. the problem I am running into is that I want the function to return the newly created File to the caller. The use block has the ability to return a value, however since it is being called with a bufferedWriter, there is no ability to actually return the File object from inside the block. Can anyone offer suggestions on how to solve this?
I want to do something like:
fun List<Contact>.buildCsv():File
{
return File("people.csv").bufferedWriter(Charsets.ISO_8859_1).use { out ->
out.write("a,b,c,d\n")
this.forEach {
out.write(it.toString())
}
}
}
but that code does not compile.
Share Improve this question edited Mar 28 at 6:08 David Soroko 9,1713 gold badges44 silver badges58 bronze badges asked Mar 26 at 20:33 pbuchheitpbuchheit 1,7012 gold badges28 silver badges63 bronze badges 3 |2 Answers
Reset to default 3You can use some builtin scope functions such as apply, also, etc.
An example:
fun List<Contact>.buildCsv() = File("people.csv").apply {
bufferedWriter(Charsets.ISO_8859_1).use { out ->
out.write("a,b,c,d\n")
forEach { out.write("$it") }
}
}
How about:
fun List<Contact>.buildCsv(): File {
val file = File("people.csv")
file.bufferedWriter(Charsets.ISO_8859_1).use { out ->
out.write("a,b,c,d\n")
this.forEach {
out.write(it.toString())
}
}
return file
}
本文标签: javaWriting and returning a File in Kotlin using a bufferStack Overflow
版权声明:本文标题:java - Writing and returning a File in Kotlin using a buffer - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744126251a2591964.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
File
in Java. It's not what you think. The functionality of that class is really to manipulate paths and the like. It really has little to do with the actual content of a file system. If you really want to return a 'file' you would do well to think of it as returning a stream to the actual content (and all that entails) – g00se Commented Mar 26 at 20:44File
s and hence the file system. It gets messy when unit testing. (You do write unit tests, right?) Instead of aFile
, pass anOutputStream
or aStreamWriter
. In a test you can pass one that collects what gets written in a byte array or string. In real life, you can still pass a stream that writes to a file. But this decouples I/O from the logic in yourbuildCsv
function. – Robert Commented Mar 26 at 22:44