How it works...

As the result of executing the preceding code, a new temp_file file is going to be created under the src/main/resources directory. Keep in mind that in case the temp_file already exists it is going to be overridden. Next, with the help of the writeText() function its contents are going to be printed to the console:

"No one in the brief history of computing has ever written a piece of perfect software. It's unlikely that you'll be the first." - Andy Hunt

The writeText() function wraps the java.io.FileOutputStream API, providing a neat way of writing content to the file. Under the hood, it accesses FileOutputStream inside the use() function, so you can be sure that it autocloses any streams that are opened during the write operation.

If the text you want to write to the file is too large to be processed at once, you can use the BufferedWriter API to allow you to write and append the file. You can easily obtain an instance of BufferedWriter using the File.bufferedWriter() extension function.

You can also pass the additional charset: Charset argument to writeText(), which by default is equal to the Charsets.UTF_8 value. If you'd like to use another charset, you can specify it by passing a proper one as the charset argument. You can find the available charset types inside the kotlin.text.Charsets object. You can also find them listed in the official documentation at https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/-charsets.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.95.102