Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.file.{Paths, StandardOpenOption}
- import fs2._
- import cats.effect.{Blocker, ExitCode, IO, IOApp}
- import cats.implicits.toFunctorOps
- object Hello extends IOApp {
- override def run(args: List[String]): IO[ExitCode] = {
- def program: Stream[IO, Unit] = {
- Stream.resource(Blocker[IO]) flatMap { blocker =>
- val stream = io.file.readAll[IO](Paths.get("/Users/macbook1/a.csv"), blocker, 4096)
- stream
- .through(text.utf8Decode) // decode bytes as UTF-8
- .through(text.lines) // get lines removing CR/LF separators
- .drop(1) // drop CSV header
- .filter(_.nonEmpty) // remove empty lines
- .map(_.replaceAll("""['"]""", "")) // remove all ' and "
- .intersperse(",") // add commas
- .through(text.utf8Encode) // encode back to bytes
- .through(io.file.writeAll(Paths.get("/Users/macbook1/b.txt"), blocker, Seq(StandardOpenOption.TRUNCATE_EXISTING)))
- }
- }
- program.compile.drain.as(ExitCode.Success)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement