I have a simple csv reader where i use to upload csv, do some manipulation on the data and print a new csv output.
Im using tototoshi csv library with Scala.
My problem is that my project knows to handle UTF-8 files, but now I need to support UTF-8-BOM file, if someone can explain me how do I solve this it will be great help.
This is the current func's that support UTF-8:
writer:
//----------------WRITER----------------//
class CsvDataWriter(csvFile: File, headers: List[String])(implicit format: CSVFormat) {
val fos = new FileOutputStream(csvFile, false)
private val writer = {
CSVWriter.open(fos, "UTF-8")(format)
}
writer.writeRow(headers)
def close() = {
fos.close()
writer.close()
}
def write(outputCSVRow: RowMap) = writer.writeRow(headers map outputCSVRow)
def writeHeaders(headers: List[String]) = {
writer.writeRow(headers)
}
}
reader:
//----------------READER----------------//
class CsvDataReader(csvFile: File) {
private val reader = CSVReader.open(csvFile, "UTF-8")(Format)
val headers: List[String] = reader.readNext().get
def close() = reader.close()
def iteratorWithHeaders: Iterator[Map[String, String]] = {
reader.iterator.map(line => headers.zip(line).toMap)
}
}
and this is the upload func when a user select the file:
def upload = Action(parse.multipartFormData) { implicit request =>
request.body.file("file").fold {
BadRequest("Missing file")
} { uploadedFile => {
val localFile = new File("/tmp/" + uploadedFile.ref.file.getName)
Files.copy(uploadedFile.ref.file.toPath, localFile.toPath, StandardCopyOption.REPLACE_EXISTING)
localFile.deleteOnExit()
val j = Json.parse( s"""{"fileId": "${Crypto.encryptAES(localFile.getAbsolutePath)}"}""")
Ok(j)
}
}
}