2-scala文件操作--自动关闭打开的资源,读取properties文件

简介

使用scala的loan pattern自动关闭打开的资源

读取properties文件

依赖的jar

使用scala_arm库自动关闭资源文件时,需要引入以下依赖:

		<dependency>
			<groupId>com.jsuereth</groupId>
			<artifactId>scala-arm_${scala.binary.version}</artifactId>
			<version>1.4</version>
		</dependency>

示例代码

import java.io.InputStream
import java.util.Properties

import scala.collection.JavaConversions.propertiesAsScalaMap

import resource.managed
import test.Control._

object Control {
  def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B =
    try {
      f(resource)
    } finally {
      if (resource != null)
        resource.close()
    }
}

object FileOperator extends App {
  val prop = new Properties()
  //读取classpath下的配置文件
  //读取properties文件
  //使用scala_arm自动关闭打开的文件资源
  for (
    source <- managed(FileOperator.getClass.getClassLoader.getResourceAsStream("db.properties"))
  ) {
    printProp(source)
  }

  //使用using自动关闭打开的文件资源
  using(FileOperator.getClass.getClassLoader.getResourceAsStream("db.properties")) { source =>
    {
      printProp(source)
    }
  }
  //使用绝对路径读取文件
  val res = readTextFile("D:/scalawork/scala-learn/src/main/resources/db.properties")
  res match {
    case Some(lines) => lines.foreach(println)
    case None        => println("couldn‘t read file")
  }

  //处理异常,返回Option
  def readTextFile(filename: String): Option[List[String]] = {
    try {
      val lines = using(io.Source.fromFile(filename)) { source =>
        (for (line <- source.getLines) yield line).toList
      }
      Some(lines)
    } catch {
      case e: Exception => None
    }
  }

  //加载properties并转换为HashMap,读取其内容
  def printProp(source: InputStream) = {
    prop.load(source)
    println(prop.getProperty("jdbc.idleMaxAge"))
    prop.foreach(ele => {
      println(s"${ele._1} => ${ele._2}")
    })
  }
}
时间: 2024-08-12 19:56:01

2-scala文件操作--自动关闭打开的资源,读取properties文件的相关文章

转载:java基础学习总结——java读取properties文件总结

java基础学习总结--java读取properties文件总结 一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的

java基础学习总结——java读取properties文件总结

一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下: 1.1.项目的目录结构 1.2. java读取properties文件代码测试

文件操作之打开文件

一.打开文件 obj = open('文件路径','打开文件方式') 打开文件方式有: r:只读方式 r+:相当于rw w:写入方式 w+:仍然等于w,无意义 a:追加方式 a+:仍然等于a,无意义 open()是Python的内建函数,提供了初始化输入/输出(I/O)的操作通用接口,成功打开一个文件后返回一个对象,否则发生IOError异常,file()是一个工厂函数,与open方法一样,可以互换,但是一般推荐用open(),因为open()是Python内建函数.file后期会被合并掉其他函

3.5 对一个文件描述符打开一个或多个文件状态标志

lib/setfl.c #include "apue.h" #include <fcntl.h> void set_fl(int fd, int flags) /* flags are file status flags to turn on */ { int val; if ((val = fcntl(fd, F_GETFL, 0)) < 0) err_sys("fcntl F_GETFL error"); val |= flags; /* tu

Java的Properties类和读取.properties文件

一..properties文件的作用 Properties属性文件在JAVA应用程序中是经常可以看得见的,也是特别重要的一类文件.它用来配置应用程序的一些信息,不过这些信息一般都是比较少的数据,没有必要使用数据库文件来保存,而使用一般的文本文件来保存,如果是通过File直接保存的话,可能在存储和读取上都不是很方便,但如果保存为Properties文件就不一样了,属性文件都有键值对应的,在JAVA的包中,有提供专门的操作属性文件的类.这个类就是 java.uitl.Properties类,由于Pr

读取properties文件并获取属性值

1.Properties与ResourceBundle 两个类都可以读取属性文件中以key/value形式存储的键值对,ResourceBundle读取属性文件时操作相对简单. 2.Properties 该类继承Hashtable,将键值对存储在集合中.基于输入流从属性文件中读取键值对,load()方法调用完毕,就与输入流脱离关系,不会自动关闭输入流,需要手动关闭. /** * 基于输入流读取属性文件:Properties继承了Hashtable,底层将key/value键值对存储在集合中, *

Java读取properties文件工具类并解决控制台中文乱码

1.建立properts文件(error.message.properties) HTTP201= 请求成功并且服务器创建了新的资源 2.在spring-mvc.xml文件(applicationContext-mvc.xml)中配置properties工具类路径及读取properties文件的路径 <bean id="propertyConfigurer" class="com.yjlc.platform.utils.PropertyConfigurer"

用java读取properties文件--转

今天为了通过java读取properties文件,google了很长时间,终于找到了.现在特记录之和大家一起分享.     下面直接贴出代码:java类 public class Mytest public static void readFile(String fileName) {//传入参数fileName是要读取的资源文件的文件名如(file.properties) InputStream in = null; Properties pros = new Properties(); tr

java读取properties文件的方法

盗亦有道: http://blog.csdn.net/lwzcjd/article/details/3116298 http://hi.baidu.com/hgd0324/item/1d5e923973b77c4d033edcaf 这里介绍两种技术:利用spring读取properties 文件和利用java.util.Properties读取(一)利用spring读取properties 文件利用org.springframework.beans.factory.support.Propert