package com.lwf.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Properties;
/*
* 从路径中的属性文件中读取单个属性或全部属性及设置属性
*/
public class GetProperty {
public static String readValue(String filePath,String key){
Properties props = new Properties();
try {
InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
props.load(ips);
String value = props.getProperty(key);
System.out.println(key+"="+value);
return value;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
//读取全部信息
public static void readProperties(String filePath) {
Properties props = new Properties();
try {
InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
props.load(ips);
Enumeration enums = props.propertyNames();
while(enums.hasMoreElements()){
String key = (String)enums.nextElement();
String value = props.getProperty(key);
System.out.println(key+"="+value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void writeProperties(String filePath,String paraKey,String paraValue){
Properties props = new Properties();
try {
OutputStream ops = new FileOutputStream(filePath);
props.setProperty(paraKey, paraValue);
props.store(ops, "set");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args){
GetProperty.readValue("D:\\sys.properties", "username");
GetProperty.readProperties("D:\\sys.properties");
GetProperty.writeProperties("D:\\sys.properties", "age", "21");
}
}
分享到:
相关推荐
JAVA 读取或写资源文件 读取properties文件
java 读取远程共享文件,一个操作简单的读取远程资源的控件。
用读取图片的例子演示在idea中如何读取资源文件。
压缩包中包含java读写hdf5格式文件时需要的jar包和动态链接库,动态链接库提供了windows、linux两种版本的
Java读取资源文件时内容过长与换行的处理
JAVA读取远程网页文件并保存本地 从远程URL地址获取网页文件下载到本地 这个方法可以生成静态HTML文件使用!
利用java 读取URL的资源,并且把读取到的资源写入到指定路径的文件中。
资源文件的创建 资源文件的结构 资源文件的读取
该资源包含了java四种方式读取和写入xml文件的代码
使用java语言,通过调用FileReader类读取txt文件中的数据。资源是可直接实现的代码。
JAVA读取配置文件(资源文件)源代码;JAVA读取配置文件(资源文件)源代码;JAVA读取配置文件(资源文件)源代码
java使用的jpoi读取execl文件 该资源含有最新的poi的jar包 可以用来读取execlxls xlsx两种格式 里面含有代码可以直接运行
该资源详细介绍了Java如何读取各种配置文件。如xml ini 等等
// 如果是,重新读取此文件 if(newTime == 0) { // 属性文件不存在 if(m_lastModifiedTime == 0){ System.err.println(PFILE+ " file does not exist!"); }else{ System.err.println(PFILE+ " file...
主要介绍了详解Java读取Jar中资源文件及实现代码的相关资料,在开发java项目的时候,经常会用到jar包,这里就说下如何读取,需要的朋友可以参考下
用JAVA语言实现的文件浏览器,类似于资源管理器的功能;
java io 文件读写
这个工程是使用 java 来使用反射的原理读取 *.proproies 资源文件中的一些配置信息。利用此方法可以在没有使用框架的情况下可以让你的程序更加灵活易于修改。如:可以将一些数据库配置信息,重要信息写入到资源文件...
本资源包换所有读取分析excel的jar包和我写的用这些包读取分析excel文件的两个.java文件。
Java读取Excel文件-附件资源