How to read/write CSV file in Kotlin » grokonez

https://grokonez.com/kotlin/kotlin-read-write-csv-file-example

How to read/write CSV file in Kotlin

In this tutorial, we’re gonna look at examples that read and write CSV file using native Kotlin.

I. Write Data to CSV File

- Simple POJO Customer (id, name, address, age):

package com.javasampleapproach.kotlin.csv

class Customer {
var id: String? = null
var name: String? = null
var address: String? = null
var age: Int = 0

constructor() {}
constructor(id: String?, name: String?, address: String?, age: Int) {
	this.id = id
	this.name = name
	this.address = address
	this.age = age
}

override fun toString(): String {
	return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]"
}

}

  • Write Customer List to CSV file:

More at:

https://grokonez.com/kotlin/kotlin-read-write-csv-file-example

How to read/write CSV file in Kotlin

#csv #kotlin #read-file #write-file

What is GEEK

Buddha Community

How to read/write CSV file in Kotlin » grokonez

How to read/write CSV file in Kotlin » grokonez

https://grokonez.com/kotlin/kotlin-read-write-csv-file-example

How to read/write CSV file in Kotlin

In this tutorial, we’re gonna look at examples that read and write CSV file using native Kotlin.

I. Write Data to CSV File

- Simple POJO Customer (id, name, address, age):

package com.javasampleapproach.kotlin.csv

class Customer {
var id: String? = null
var name: String? = null
var address: String? = null
var age: Int = 0

constructor() {}
constructor(id: String?, name: String?, address: String?, age: Int) {
	this.id = id
	this.name = name
	this.address = address
	this.age = age
}

override fun toString(): String {
	return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]"
}

}

  • Write Customer List to CSV file:

More at:

https://grokonez.com/kotlin/kotlin-read-write-csv-file-example

How to read/write CSV file in Kotlin

#csv #kotlin #read-file #write-file

Kotlin - How to read/write Excel file with Apache POI » grokonez

https://grokonez.com/kotlin/kotlin-read-write-excel-file-apache-poi-example

Kotlin – How to read/write Excel file with Apache POI

In this tutorial, we’re gonna look at Kotlin examples that read and write Excel file using Apache POI.

I. Dependency

<dependency>
	<groupId>org.jetbrains.kotlin</groupId>
	<artifactId>kotlin-stdlib</artifactId>
	<version>1.2.21</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>

II. Write Data to Excel File

- Simple POJO Customer (id, name, address, age):

package com.javasampleapproach.kotlin.apachecsv

class Customer {
var id: String? = null
var name: String? = null
var address: String? = null
var age: Int = 0

constructor() {}
constructor(id: String?, name: String?, address: String?, age: Int) {
	this.id = id
	this.name = name
	this.address = address
	this.age = age
}

override fun toString(): String {
	return "Customer [id=" + id + ", name=" + name + ", address=" + address + ", age=" + age + "]"
}

}

  • Write to Excel file:

More at:
https://grokonez.com/kotlin/kotlin-read-write-excel-file-apache-poi-example

Kotlin – How to read/write Excel file with Apache POI

#kotlin #apache-poi #excel #read-file #write-file

Kotlin Properties - Read/Write Properties from/to .properties/.XML File - ozenero

https://ozenero.com/kotlin-properties-read-write-properties-file-properties-xml-file

Kotlin Properties – Read/Write Properties from/to .properties/.XML File

In the post, we show how to Read/Write Properties from/to .Properties/.XML files by Kotlin language.

I. Kotlin - Read/Write Properties from/to .Properties file

1. Write Properties to .Properties file
1.1 Properties store() method
We use java.util.Properties.store() methods:

// 1.
fun store(out: OutputStream, comments: String): Unit

-> Writes this property list (key and element pairs) in this Properties table 
to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method.

// 2.
fun store(writer: Writer, comments: String): Unit

-> Writes this property list (key and element pairs) in this Properties table 
to the output character stream in a format suitable for using the load(Reader) method.
1.2 Kotlin Program – write Properties to .properties file

More at:

https://ozenero.com/kotlin-properties-read-write-properties-file-properties-xml-file

Kotlin Properties – Read/Write Properties from/to .properties/.XML File

#kotlin #xml #read-file #write-file

How to read/write CSV files in Python » grokonez

https://grokonez.com/python/how-to-read-write-csv-files-in-python-csv-module

How to read/write CSV files in Python

In this tutorial, we’re gonna look at way to use csv module to read, write CSV files in Python program.

CSV files

CSV stands for 'comma-separated values'. CSV file is a spreadsheet stored as plain-text file. Each line in a CSV file represents a row, and commas separate cells in the row.

For example:


Title,Category,Date
Python String methods,Python,12/27/2018
How to iterate over a List in Java,Java,12/21/2018
Kotlin List functions,Kotlin,12/19/2018

Instead of commas, cells could be separated with tab characters:


Title	Tags	Date
Python String methods	python, string	12/27/2018
How to iterate over a List in Python	python, list	12/21/2018
Python List functions	python, list	12/19/2018

CSV file is simple than Excel spreadsheet because it doesn’t have:

  • types, everything is just a string
  • font size or color
  • multiple worksheets
  • cell widths and heights
  • merge cells
  • images or charts

Read CSV file in Python

You can follow these steps to read data from a CSV file: - import csv module - open CSV file using open() function - create a Reader object that lets you iterate over rows - access values in the Reader object by: + convert it to a plain Python list using list() function + use for-loop (avoid loading the entire file into memory at once)

# cells are separated with commas
>>> import csv
>>> gkzFile = open('grokonez.csv')
>>> csvReader = csv.reader(gkzFile)
>>> gkzTuts = list(csvReader)
>>> gkzTuts
[['Title', 'Category', 'Date'], ['Python String methods', 'Python', '12/27/2018'], ['How to iterate over a List in Java', 'Java', '12/21/2018'], ['Kotlin List functions', 'Kotlin', '12/19/2018']]
>>> gkzTuts[0][0] # data[row][col]
'Title'
>>> gkzTuts[0][1]
'Category'
>>> gkzTuts[0][2]
'Date'
>>> gkzTuts[2][0]
'How to iterate over a List in Java'
>>> gkzTuts[2][1]
'Java'
>>> gkzTuts[2][2]
'12/21/2018'

cells are separated with ‘tab’ characters

gkzFile = open(‘grokonez.csv’)
csvReader = csv.reader(gkzFile, delimiter=‘\t’)
gkzTuts = list(csvReader)
gkzTuts
[[‘Title’, ‘Tags’, ‘Date’], [‘Python String methods’, ‘python, string’, ‘12/27/2018’], [‘How to iterate over a List in Python’, ‘python, list’, ‘12/21/2018’], [‘Python List functions’, ‘python, list’, ‘12/19/2018’]]
gkzTuts[0][0]
‘Title’
gkzTuts[0][1]
‘Tags’
gkzTuts[0][2]
‘Date’
gkzTuts[2][0]
‘How to iterate over a List in Python’
gkzTuts[2][1]
‘python, list’
gkzTuts[2][2]
‘12/21/2018’


Remember that we can loop over Reader object only once.
To reread the CSV file by for-loop, we must create Reader object again.

More at:

https://grokonez.com/python/how-to-read-write-csv-files-in-python-csv-module

How to read/write CSV files in Python

#python #csv #read #write

Kotlin - How to read/write CSV file with OpenCSV » grokonez

https://grokonez.com/kotlin/kotlin-how-to-read-write-csv-file-with-opencsv-example

Kotlin – How to read/write CSV file with OpenCSV

In this tutorial, we’re gonna look at Kotlin examples that read and write CSV file using OpenCSV.

I. Dependency

<dependency>
	<groupId>org.jetbrains.kotlin</groupId>
	<artifactId>kotlin-stdlib</artifactId>
	<version>1.2.21</version>
</dependency>

<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>4.0</version>
</dependency>

II. Write Data to CSV File

1. From String Array


var fileWriter = FileWriter("customer.csv")

csvWriter = CSVWriter(fileWriter,
CSVWriter.DEFAULT_SEPARATOR,
CSVWriter.NO_QUOTE_CHARACTER,
CSVWriter.DEFAULT_ESCAPE_CHARACTER,
CSVWriter.DEFAULT_LINE_END)

val data = arrayOf(“ID”,“NAME”,“ADDRESS”,“AGE”)
csvWriter.writeNext(data)

2. From List of Objects

More at:

https://grokonez.com/kotlin/kotlin-how-to-read-write-csv-file-with-opencsv-example

Kotlin – How to read/write CSV file with OpenCSV

#kotlin #csv-file #opencsv