All Downloads are FREE. Search and download functionalities are using the official Maven repository.

a8.shared.ops.PathOps.scala Maven / Gradle / Ivy

There is a newer version: 1.0.0-20230212_1012_master
Show newest version
package a8.shared.ops

import java.nio.charset.StandardCharsets._
import java.nio.file.attribute.FileAttribute
import java.nio.file.{Files, Path, Paths}

object PathOps {

  def readFile(file: java.io.File): String = {
    new String(Files.readAllBytes(Paths.get(file.getCanonicalPath)), UTF_8)
  }

  def fileExtension(file: java.io.File): String = {
    val name = file.getName
    name.lastIndexOf(".") match {
      case i if i >= 0 && i < name.length =>
        file.getName.substring(i+1)
      case _ =>
        ""
    }
  }

}


class PathOps(private val path: java.nio.file.Path) extends AnyVal {

  def absNormalize(): Path =
    path
      .toAbsolutePath
      .normalize()

  def subdir(name: String): java.nio.file.Path =
    path.resolve(Paths.get(name))

  def exists() = Files.exists(path)

  def isFile() = Files.isRegularFile(path)
  def isDirectory() = Files.isDirectory(path)

  def parentOpt() = Option(path.getParent)

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy