alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (Files.scala)

This example Play Framework source code file (Files.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

api, boolean, file, fileoutputstream, files, io, java, lib, library, outputstreamwriter, play, play framework, string, temporaryfile, use, utility

The Files.scala Play Framework example source code

/*
 * Copyright (C) 2009-2013 Typesafe Inc. <http://www.typesafe.com>
 */
package play.api.libs

import java.io._
import play.utils.PlayIO
import scala.io.Codec

/**
 * FileSystem utilities.
 */
object Files {

  /**
   * A temporary file hold a reference to a real file, and will delete
   * it when the reference is garbaged.
   */
  case class TemporaryFile(file: File) {

    /**
     * Clean this temporary file now.
     */
    def clean(): Boolean = {
      file.delete()
    }

    /**
     * Move the file.
     */
    def moveTo(to: File, replace: Boolean = false) {
      Files.moveFile(file, to, replace = replace)
    }

    /**
     * Delete this file on garbage collection.
     */
    override def finalize {
      clean()
    }

  }

  /**
   * Utilities to manage temporary files.
   */
  object TemporaryFile {

    /**
     * Create a new temporary file.
     *
     * Example:
     * {{{
     * val tempFile = TemporaryFile(prefix = "uploaded")
     * }}}
     *
     * @param prefix The prefix used for the temporary file name.
     * @param suffix The suffix used for the temporary file name.
     * @return A temporary file instance.
     */
    def apply(prefix: String = "", suffix: String = ""): TemporaryFile = {
      new TemporaryFile(File.createTempFile(prefix, suffix))
    }

  }

  /**
   * Copy a file.
   */
  @deprecated("Use Java 7 Files API instead", "2.3")
  def copyFile(from: File, to: File, replaceExisting: Boolean = true): File = {
    if (replaceExisting || !to.exists()) {
      val in = new FileInputStream(from).getChannel
      try {
        val out = new FileOutputStream(to).getChannel
        try {
          out.transferFrom(in, 0, in.size())
        } finally {
          PlayIO.closeQuietly(out)
        }
      } finally {
        PlayIO.closeQuietly(in)
      }
    }

    to
  }

  /**
   * Rename a file.
   */
  @deprecated("Use Java 7 Files API instead", "2.3")
  def moveFile(from: File, to: File, replace: Boolean = true): File = {
    if (to.exists() && replace) {
      to.delete()
    }

    if (!to.exists()) {
      if (!from.renameTo(to)) {
        copyFile(from, to)
        from.delete()
      }
    }

    to
  }

  /**
   * Reads a file’s contents into a String.
   *
   * @param path the file to read.
   * @return the file contents
   */
  @deprecated("Use Java 7 Files API instead", "2.3")
  def readFile(path: File): String = PlayIO.readFileAsString(path)(Codec.UTF8)

  /**
   * Write a file’s contents as a `String`.
   *
   * @param path the file to write to
   * @param content the contents to write
   */
  @deprecated("Use Java 7 Files API instead", "2.3")
  def writeFile(path: File, content: String): Unit = {
    path.getParentFile.mkdirs()
    val out = new FileOutputStream(path)
    try {
      val writer = new OutputStreamWriter(out, Codec.UTF8.name)
      try {
        writer.write(content)
      } finally PlayIO.closeQuietly(writer)
    } finally PlayIO.closeQuietly(out)
  }

  /**
   * Creates a directory.
   *
   * @param path the directory to create
   */
  @deprecated("Use Java 7 Files API instead", "2.3")
  def createDirectory(path: File): File = {
    path.mkdirs()
    path
  }

  /**
   * Writes a file’s content as String, only touching the file if the actual file content is different.
   *
   * @param path the file to write to
   * @param content the contents to write
   */
  @deprecated("Use Java 7 Files API instead", "2.3")
  def writeFileIfChanged(path: File, content: String) {
    if (content != Option(path).filter(_.exists).map(readFile(_)).getOrElse("")) {
      writeFile(path, content)
    }
  }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework Files.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.