当前位置: 首页>>代码示例>>Scala>>正文


Scala StandardOpenOption类代码示例

本文整理汇总了Scala中java.nio.file.StandardOpenOption的典型用法代码示例。如果您正苦于以下问题:Scala StandardOpenOption类的具体用法?Scala StandardOpenOption怎么用?Scala StandardOpenOption使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了StandardOpenOption类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: SnapshotLogger

//设置package包名称以及导入依赖的类
package se.gigurra.dcs.remote.util

import java.nio.file.{Files, Paths, StandardOpenOption}
import com.twitter.finagle.util.DefaultTimer
import com.twitter.io.Charsets
import com.twitter.util.Duration

case class SnapshotLogger(outputFilePath: String,
                          dtFlush: Duration,
                          enabled: Boolean,
                          fGetSnapshot: () => String) {

  private val path = Paths.get(outputFilePath)

  if (enabled) {
    DefaultTimer.twitter.schedule(dtFlush) {
      Files.write(path, fGetSnapshot().getBytes(Charsets.Utf8), StandardOpenOption.WRITE, StandardOpenOption.CREATE)
    }
  }

} 
开发者ID:GiGurra,项目名称:dcs-remote2,代码行数:22,代码来源:SnapshotLogger.scala

示例2: DataUseLogger

//设置package包名称以及导入依赖的类
package se.gigurra.dcs.remote.util

import java.nio.file.{Files, Paths, StandardOpenOption}
import java.util.concurrent.atomic.AtomicLong
import com.twitter.finagle.util.DefaultTimer
import com.twitter.io.Charsets
import com.twitter.util.Duration
import scala.util.Try
import scala.collection.JavaConversions._

case class DataUseLogger(outputFilePath: String, dtFlush: Duration, enabled: Boolean) {

  private val atomicDelta = new AtomicLong(0L)
  private val path = Paths.get(outputFilePath)

  if (enabled) {
    DefaultTimer.twitter.schedule(dtFlush) {
      val delta = atomicDelta.getAndSet(0)
      val prevBytes = Try(Files.readAllLines(path, Charsets.Utf8).head.toLong).getOrElse(0L)
      val newVal = ((prevBytes + delta).toString + "\n").getBytes(Charsets.Utf8)
      Files.write(path, newVal, StandardOpenOption.WRITE, StandardOpenOption.CREATE)
    }
  }

  def log(nBytes: Long): Unit = {
    atomicDelta.addAndGet(nBytes)
  }

} 
开发者ID:GiGurra,项目名称:dcs-remote2,代码行数:30,代码来源:DataUseLogger.scala

示例3: TorrentFileStream

//设置package包名称以及导入依赖的类
package com.spooky.bencode

import scala.collection.JavaConversions._
import java.io.File
import java.nio.channels.FileChannel
import java.nio.file.StandardOpenOption
import java.nio.channels.FileChannel.MapMode
import java.nio.ByteBuffer
import scala.Range

class TorrentFileStream(channel: FileChannel, buffer: ByteBuffer) extends BStream {
  def headChar: Char = buffer.duplicate.get.asInstanceOf[Char]
  def headByte: Byte = buffer.duplicate.get
  def tail = {
    val tail = buffer.duplicate
    tail.get
    new TorrentFileStream(channel, tail)
  }
  def isEmpty = !buffer.hasRemaining
  def close: Unit = channel.close
  override def toString: String = {
    val buff = buffer.duplicate
    val builder = StringBuilder.newBuilder
    while (buff.hasRemaining) {
      if (builder.endsWith("6:pieces")) {
        val bah = StringBuilder.newBuilder
        var chaaa = buff.get.asInstanceOf[Char]
        while ("0123456789".contains(chaaa)) {
          bah.append(chaaa)
          chaaa = buff.get.asInstanceOf[Char]
        }
        var i = bah.toString.toInt
        while(i >= 0){
          buff.get
          i = i-1
        }
      }
      builder += buff.get.asInstanceOf[Char]
    }
    builder.toString
  }
}
object TorrentFileStream {
  def apply(torrent: File) = {
    val channel = FileChannel.open(torrent.toPath, StandardOpenOption.READ)
    new TorrentFileStream(channel, channel.map(MapMode.READ_ONLY, 0, channel.size).load)
  }
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:49,代码来源:TorrentFileStream.scala

示例4: RandomFile

//设置package包名称以及导入依赖的类
package com.spooky.bittorrent

import java.nio.channels.FileChannel
import java.io.File
import java.nio.file.StandardOpenOption
import scala.util.Random
import java.nio.ByteBuffer
import com.spooky.bittorrent.u.GigaByte

object RandomFile {
  def main(args: Array[String]): Unit = {
    val channel = FileChannel.open(new File("O:\\tmp\\file.dump").toPath, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    val r = new Random(0)
    val buffer = ByteBuffer.allocate(1024 * 4)
    val bytes = GigaByte(50).toBytes
    println(bytes.capacity.toLong)
    for (_ <- 0l to(bytes.capacity.toLong, buffer.capacity.toLong)) {
      r.nextBytes(buffer.array())
      buffer.limit(buffer.capacity)
      buffer.position(0)
      channel.write(buffer)
    }
    channel.close()
  }
} 
开发者ID:zpooky,项目名称:bittorrent,代码行数:26,代码来源:RandomFile.scala

示例5: NpyFileSpec

//设置package包名称以及导入依赖的类
import java.nio.channels.FileChannel
import java.nio.charset.StandardCharsets
import java.nio.file.{Paths, StandardOpenOption}
import java.nio.{ByteBuffer, ByteOrder}
import sys.process._
import scala.collection.immutable.Range.Inclusive
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

import org.scalatest.FlatSpec
import com.indix.ml2npy.NpyFile


class NpyFileSpec extends FlatSpec{
  val nosetestspath="nosetests "
  val pathToTest = getClass.getResource("/python/Npytest.py").getPath+":"
  "NpyFile" should "Convert a float array correctly" in {
    val content = NpyFile[Float].addElements(Seq(0.3f, 0.5f))
    val channel = FileChannel.open(Paths.get("/tmp/test.npy"), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
    channel.write(content)
    channel.close()
    val command=nosetestspath + pathToTest+"test_0"
    val response=command.!
    assert(response==0)
  }

  "NpyFile" should "Convert a sequence of integers correctly" in {
    val intContent = NpyFile[Int].addElements(1 to 10)
    val intChannel = FileChannel.open(Paths.get("/tmp/inttest.npy"), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
    intChannel.write(intContent)
    intChannel.close()
    val command=nosetestspath + pathToTest+"test_1"
    val response=command.!
    assert(response==0)
  }

  "NpyFile" should "write bytes directly" in {
    val data: Inclusive = 1 to 10
    val intContent3 = NpyFile[Int]
    data.foreach(intContent3.addToBuffer)
    val bytes = intContent3.getBytes
    val intChannel3 = FileChannel.open(Paths.get("/tmp/inttest3.npy"), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
    intChannel3.write(ByteBuffer.wrap(bytes))
    intChannel3.close()
    val command=nosetestspath + pathToTest+"test_3"
    val response=command.!
    assert(response==0)
  }
} 
开发者ID:indix,项目名称:ml2npy,代码行数:50,代码来源:NpyFileSpec.scala

示例6: FileHelper

//设置package包名称以及导入依赖的类
package codacy.dockerApi.utils

import java.io.File
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Path, Paths, StandardOpenOption}

object FileHelper {

  def createTmpFile(content: String, prefix: String = "config", suffix: String = ".conf"): Path = {
    Files.write(
      Files.createTempFile(prefix, suffix),
      content.getBytes(StandardCharsets.UTF_8),
      StandardOpenOption.CREATE
    )
  }

  def stripPath(filename: Path, prefix: Path): String = {
    stripPath(filename.toString, prefix.toString)
  }

  def stripPath(filename: String, prefix: String): String = {
    filename.stripPrefix(prefix)
      .stripPrefix("/")
  }

  def listAllFiles(path: String): List[File] = {
    listAllFiles(Paths.get(path))
  }

  def listAllFiles(path: Path): List[File] = {
    recursiveListFiles(path.toFile)
  }

  private def recursiveListFiles(file: File): List[File] = {
    val these = file.listFiles
    (these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles)).toList
  }

} 
开发者ID:codacy,项目名称:codacy-duplication-scala-seed,代码行数:40,代码来源:FileHelper.scala

示例7: Main

//设置package包名称以及导入依赖的类
package com.harry0000.kancolle.ac

import java.io.File
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.{Files, StandardOpenOption}

object Main {

  def main(args: Array[String]): Unit = {
    val path = Config.distPath
    val areas = PlaceCrawler.crawl()

    writeCSV(path, "place.csv", areas)
    writeJSON(path, "place.json", areas)
  }

  val headers = Seq("area", "prefecture_code", "prefecture_name", "place_name", "place_address")

  def writeCSV(path: String, name: String, areas: Seq[Area]): Unit = {
    import com.github.tototoshi.csv.CSVWriter

    val writer = CSVWriter.open(new File(path, name))
    try {
      writer.writeRow(headers)

      for {
        area  <- areas
        pref  <- area.prefectures
        place <- pref.places
      } {
        writer.writeRow(Seq(area.name, pref.code, pref.name, place.name, place.address))
      }
    } finally {
      writer.close()
    }
  }

  def writeJSON(path: String, name: String, areas: Seq[Area]): Unit = {
    import spray.json._
    import PlaceCrawler._

    val writer = Files.newBufferedWriter(new File(path, name).toPath, UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)
    try {
      writer.write(areas.toJson.prettyPrint)
    } finally {
      writer.close()
    }
  }
} 
开发者ID:harry0000,项目名称:KancolleAnchor,代码行数:50,代码来源:Main.scala

示例8: FileIO

//设置package包名称以及导入依赖的类
package swave.core.io.files

import java.io.File
import java.nio.channels.FileChannel
import java.nio.file.{FileSystems, Files, Path, StandardOpenOption}

import scala.util.control.NonFatal
import com.typesafe.config.Config
import swave.core.impl.util.SettingsCompanion
import swave.core.io.Bytes
import swave.core.macros._

object FileIO extends SpoutFromFiles with DrainToFiles {

  lazy val userHomePath: Path = FileSystems.getDefault.getPath(System getProperty "user.home")

  def resolveFileSystemPath(pathName: String): Path =
    if (pathName.length >= 2 && pathName.charAt(0) == '~' && pathName.charAt(1) == File.separatorChar) {
      userHomePath.resolve(pathName substring 2)
    } else FileSystems.getDefault.getPath(pathName)

  val WriteCreateOptions: Set[StandardOpenOption] = {
    import StandardOpenOption._
    Set(CREATE, TRUNCATE_EXISTING, WRITE)
  }

  final case class Settings(defaultFileReadingChunkSize: Int, defaultFileWritingChunkSize: Int) {
    requireArg(defaultFileReadingChunkSize > 0, "`defaultFileChunkSize` must be > 0")
    requireArg(defaultFileWritingChunkSize >= 0, "`defaultFileWritingChunkSize` must be >= 0")

    def withDefaultFileReadingChunkSize(defaultFileReadingChunkSize: Int) =
      copy(defaultFileReadingChunkSize = defaultFileReadingChunkSize)
    def withDefaultFileWritingChunkSize(defaultFileWritingChunkSize: Int) =
      copy(defaultFileWritingChunkSize = defaultFileWritingChunkSize)
  }

  object Settings extends SettingsCompanion[Settings]("swave.core.file-io") {
    def fromSubConfig(c: Config): Settings =
      Settings(
        defaultFileReadingChunkSize = c getInt "default-file-reading-chunk-size",
        defaultFileWritingChunkSize = c getInt "default-file-writing-chunk-size")
  }

  def writeFile[T: Bytes](fileName: String, data: T): Unit = writeFile(resolveFileSystemPath(fileName), data)
  def writeFile[T: Bytes](file: File, data: T): Unit       = writeFile(file.toPath, data)
  def writeFile[T: Bytes](path: Path, data: T, options: StandardOpenOption*): Unit = {
    implicit def decorator(value: T): Bytes.Decorator[T] = Bytes.decorator(value)
    Files.write(path, data.toArray, options: _*)
    ()
  }

  def readFile[T: Bytes](fileName: String): T = readFile(resolveFileSystemPath(fileName))
  def readFile[T: Bytes](file: File): T       = readFile(file.toPath)
  def readFile[T: Bytes](path: Path): T       = implicitly[Bytes[T]].apply(Files.readAllBytes(path))

  private[io] def quietClose(channel: FileChannel): Unit =
    try channel.close()
    catch { case NonFatal(_) ? }
} 
开发者ID:sirthias,项目名称:swave,代码行数:60,代码来源:FileIO.scala

示例9: Boot

//设置package包名称以及导入依赖的类
package definiti.scalamodel

import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths, StandardOpenOption}

import definiti.core._

import scala.collection.JavaConverters._

object Boot extends App {
  try {
    val configuration = Configuration(
      source = Paths.get("src", "main", "resources", "samples", "first.def"),
      core = CoreConfiguration(
        source = Paths.get("src", "main", "resources", "api")
      )
    )
    val destination = Paths.get("target", "samples", "result.scala")

    val project = new Project(configuration)
    project.load() match {
      case Left(errors) =>
        errors.foreach(System.err.println)
      case Right(projectResult) =>
        val root = projectResult.root
        implicit val contexte = projectResult.context
        val result = ScalaASTBuilder.build(root)
        Files.createDirectories(destination.getParent)
        Files.write(destination, Seq(result).asJava, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)
    }
  } catch {
    // In some cases, an Exception is thrown because the parser do not recognize an expression and crash its tree.
    // Did not happened with a successful syntax yet.
    case e: Exception =>
      e.printStackTrace()
  }
} 
开发者ID:definiti,项目名称:definiti-scala-model,代码行数:38,代码来源:Boot.scala

示例10: FolderLock

//设置package包名称以及导入依赖的类
package fresco.util

import java.nio.channels.{FileChannel, FileLock}
import java.nio.file.{Files, OpenOption, Paths, StandardOpenOption}


class FolderLock  (folder: => String) extends Lock {
  var isLocked = false
  lazy val lockFolder = Paths.get(folder)
  lazy val lockFilePath = Paths.get(folder, ".lock")
  var channel: FileChannel = null
  var flock: FileLock = null
  override def lock(): Lock = {
    try {
      this synchronized {
        if(isLocked) return this
        if(Files.notExists(lockFolder)) Files.createDirectories(lockFolder)
        if(Files.notExists(lockFilePath)) Files.createFile(lockFilePath)
        logger.debug(s"created file $lockFilePath")
        channel = FileChannel.open(lockFilePath, StandardOpenOption.CREATE, StandardOpenOption.WRITE)
        flock = channel.lock()
        logger.debug(s"acquired lock")
        isLocked = true
        this
      }
    } catch {
      case ex: Exception => {
        logger.error(s"exception while locking: $ex")
        if(isLocked) release()
        throw ex
      }
    }
  }

  override def tryLock(): Boolean =  {
    try {
      lock()
      true
    } catch {
      case ex: Exception => false
    }
  }

  override def release(): Unit = {
    try {
      this synchronized {
        if(!isLocked) return
        if (flock != null) flock.release()
        if (channel != null && channel.isOpen) channel.close()
        if (Files.exists(lockFilePath)) Files.delete(lockFilePath)
        logger.debug(s"lock released $lockFilePath")
        isLocked = false
      }
    } catch {
      case ex: Exception => {
        logger.error(s"exception while releasing lock: $ex")
      } //TODO: handle exception
    }
  }
} 
开发者ID:ksuhail7,项目名称:eCabinet,代码行数:61,代码来源:FolderLock.scala

示例11: FileDownloader

//设置package包名称以及导入依赖的类
package it.milczarek.gpwquoter.file

import java.io.File
import java.net.URL
import java.nio.file.{Files, Path, Paths, StandardOpenOption}

import it.milczarek.gpwquoter.AppConfig
import org.apache.http.client.fluent.Request
import org.slf4j.LoggerFactory

import scala.util.{Failure, Success, Try}


class FileDownloader(appConfig: AppConfig) {

  private val logger = LoggerFactory.getLogger(classOf[FileDownloader])

  def download(url: URL): Try[File] = {
    try {
      logger.info(s"Download file: $url")
      val response = Request.Get(url.toString).execute().returnContent().asBytes()
      val file = Files.write(outputFile(url), response, StandardOpenOption.CREATE).toFile
      Success(file)
    } catch {
      case e: Throwable => Failure(e)
    }
  }

  def outputFile(url: URL): Path = {
    val dir = new File(appConfig.dataFilesLocation)
    if (!dir.exists) dir.mkdirs()
    val uri = url.getFile
    val startFileName = uri.lastIndexOf("/") + 1
    Paths.get(appConfig.dataFilesLocation, uri.substring(startFileName))
  }

} 
开发者ID:milczarekIT,项目名称:gpw-quoter,代码行数:38,代码来源:FileDownloader.scala

示例12: DbService

//设置package包名称以及导入依赖的类
package me.snov.sns.service

import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths, StandardOpenOption}

import akka.event.LoggingAdapter
import me.snov.sns.model.Configuration
import spray.json._

class DbService(dbFilePath: String)(implicit log: LoggingAdapter) {

  val subscriptionsName = "subscriptions"
  val topicsName = "topics"
  
  val path = Paths.get(dbFilePath)
  
  def load(): Option[Configuration] = {
    if (Files.exists(path)) {
      log.debug("Loading DB")
      try {
        val configuration = read().parseJson.convertTo[Configuration]
        log.info("Loaded DB")
        return Some(configuration)
      } catch {
        case e: DeserializationException => log.warning("Unable to parse configuration")
        case e: RuntimeException => log.warning("Unable to load configuration")
      }
    }
    None
  }
  
  def save(configuration: Configuration) = {
    log.debug("Saving DB")
    write(configuration.toJson.prettyPrint)
  }

  private def write(contents: String) = {
    Files.write(path, contents.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)
  }

  private def read(): String = {
    new String(Files.readAllBytes(path))
  }
} 
开发者ID:s12v,项目名称:sns,代码行数:45,代码来源:DbService.scala

示例13: ResourceHelper

//设置package包名称以及导入依赖的类
package com.codacy.dotnet.helpers

import java.io.InputStream
import java.nio.charset.{CodingErrorAction, StandardCharsets}
import java.nio.file.{Files, Path, StandardOpenOption}

import scala.io.Codec
import scala.util.{Failure, Try}

object ResourceHelper {

  implicit val codec = Codec("UTF-8")
  codec.onMalformedInput(CodingErrorAction.REPLACE)
  codec.onUnmappableCharacter(CodingErrorAction.REPLACE)

  def getResourceStream(path: String): Try[InputStream] = {
    Option(getClass.getClassLoader.getResource(path)).map { url =>
      Try(url.openStream())
    }.getOrElse {
      Failure(new Exception("The path provided was not found"))
    }
  }

  def writeFile(path: Path, content: String): Try[Path] = {
    Try(Files.write(
      path,
      content.getBytes(StandardCharsets.UTF_8),
      StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE
    ))
  }

} 
开发者ID:codacy,项目名称:codacy-sonar-csharp,代码行数:33,代码来源:ResourceHelper.scala

示例14: BazLoader

//设置package包名称以及导入依赖的类
package impl

import java.nio.file.{Files, StandardOpenOption}
import java.util.Date

import com.lightbend.lagom.scaladsl.api.ServiceLocator.NoServiceLocator
import com.lightbend.lagom.scaladsl.server._
import com.lightbend.lagom.scaladsl.devmode.LagomDevModeComponents
import play.api.libs.ws.ahc.AhcWSComponents
import api.BazService
import com.softwaremill.macwire._

class BazLoader extends LagomApplicationLoader {

  override def load(context: LagomApplicationContext): LagomApplication =
    new BazApplication(context) {
      override def serviceLocator = NoServiceLocator
    }

  override def loadDevMode(context: LagomApplicationContext): LagomApplication =
    new BazApplication(context) with LagomDevModeComponents
}

abstract class BazApplication(context: LagomApplicationContext)
  extends LagomApplication(context)
    with AhcWSComponents {

  override lazy val lagomServer =serverFor[BazService](wire[BazServiceImpl])


  Files.write(environment.getFile("target/reload.log").toPath, s"${new Date()} - reloaded\n".getBytes("utf-8"),
    StandardOpenOption.CREATE, StandardOpenOption.APPEND)
} 
开发者ID:lagom,项目名称:lagom,代码行数:34,代码来源:BazLoader.scala

示例15: BarLoader

//设置package包名称以及导入依赖的类
package impl

import java.nio.file.{Files, StandardOpenOption}
import java.util.Date

import com.lightbend.lagom.scaladsl.api.ServiceLocator.NoServiceLocator
import com.lightbend.lagom.scaladsl.server._
import com.lightbend.lagom.scaladsl.devmode.LagomDevModeComponents
import play.api.libs.ws.ahc.AhcWSComponents
import api.{BarService, FooService}
import com.softwaremill.macwire._

class BarLoader extends LagomApplicationLoader {

  override def load(context: LagomApplicationContext): LagomApplication =
    new BarApplication(context) {
      override def serviceLocator = NoServiceLocator
    }

  override def loadDevMode(context: LagomApplicationContext): LagomApplication =
    new BarApplication(context) with LagomDevModeComponents
}

abstract class BarApplication(context: LagomApplicationContext)
  extends LagomApplication(context)
    with AhcWSComponents {

  override lazy val lagomServer = serverFor[BarService](wire[BarServiceImpl])


  lazy val fooService = serviceClient.implement[FooService]

  Files.write(environment.getFile("target/reload.log").toPath, s"${new Date()} - reloaded\n".getBytes("utf-8"),
    StandardOpenOption.CREATE, StandardOpenOption.APPEND)
} 
开发者ID:lagom,项目名称:lagom,代码行数:36,代码来源:BarLoader.scala


注:本文中的java.nio.file.StandardOpenOption类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。