java读取properties文件|在java中如何读取properties文件

java读取properties文件|在java中如何读取properties文件的第1张示图

① java中怎么读取properties文件

(1)load(InputStream inStream)这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:Properties pro = new Properties();FileInputStream in = new FileInputStream("a.properties");pro.load(in);in.close();(2)store(OutputStream out, String comments)这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:FileOutputStream oFile = new FileOutputStream(file, "a.properties");pro.store(oFile, "Comment");oFile.close();

② 求用java读写properties文件的代码

Java可使用Properties类读写properties,具体说明如下:1.Properties类与Properties配置文件Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。2.Properties中的主要方法(1)load(InputStream inStream) 这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象。如下面的代码:Properties pro = new Properties();FileInputStream in = new FileInputStream("a.properties");pro.load(in);in.close();(2)store(OutputStream out, String comments) 这个方法将Properties类对象的属性列表保存到输出流中。如下面的代码:FileOutputStream oFile = new FileOutputStream(file, "a.properties");pro.store(oFile, "Comment");oFile.close();如果comments不为空,保存后的属性文件第一行会是#comments,表示注释信息;如果为空则没有注释信息。注释信息后面是属性文件的当前保存时间信息。(3)getProperty/setProperty 这两个方法是分别是获取和设置属性信息。3.代码实例属性文件a.properties如下:name=rootpass=liukey=value读取a.properties属性列表,与生成属性文件b.properties。代码如下:import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.util.Iterator;import java.util.Properties;public class PropertyTest { public static void main(String[] args) { try { // 读取属性文件a.properties InputStream in = new BufferedInputStream(new FileInputStream("a.properties")); // /加载属性列表 Properties prop = new Properties(); prop.load(in); Iterator<String> it = prop.stringPropertyNames().iterator(); while (it.hasNext()) { String key = it.next(); System.out.println(key + ":" + prop.getProperty(key)); } in.close(); // /保存属性到b.properties文件 FileOutputStream oFile = new FileOutputStream("b.properties", true);// true表示追加打开 prop.setProperty("phone", "10086"); prop.store(oFile, "The New properties file"); oFile.close(); } catch (Exception e) { System.out.println(e); } }}

③ java如何读取properties文件

../conf/config.properties 还有src是程序运行的根目录,建议把所有与程序相关的都放到src里.不要放在外面

④ java读取properties文件问题

把properties文件中的值读取到内存对象Properties中,方便使用;

⑤ JAVA中如何读取src下所有的properties文件

最常用读取properties文件的方法InputStream in = getClass().getResourceAsStream("资源Name");这种方式要求properties文件和当前类在同一文件夹下面。如果在不同的包中,必须使用:InputStream ins = this.getClass().getResourceAsStream("/cn/zhao/properties/testPropertiesPath2.properties");Java中获取路径方法获取路径的一个简单实现反射方式获取properties文件的三种方式1 反射方式获取properties文件最常用方法以及思考:Java读取properties文件的方法比较多,网上最多的文章是"Java读取properties文件的六种方法",但在Java应用中,最常用还是通过java.lang.Class类的getResourceAsStream(String name) 方法来实现,但众多读取properties文件的代码中,都会这么做:InputStream in = getClass().getResourceAsStream("资源Name");这里面有个问题,就是getClass()调用的时候默认省略了this,this是不能在static(静态)方法或者static块中使用的,原因是static类型的方法或者代码块是属于类本身的,不属于某个对象,而this本身就代表当前对象,而静态方法或者块调用的时候是不用初始化对象的。问题是:假如不想让某个类有对象,那么会将此类的默认构造方法设为私有,当然也不会写别的共有的构造方法。并且我这个类是工具类,都是静态的方法和变量,要在静态块或者静态方法中获取properties文件,这个方法就行不通了。其实这个类就不是这么用的,他仅仅是需要获取一个Class对象就可以了,那就容易了,取所有类的父类Object,用Object.class比用正在写类自身方便安全,下面给出一个例子,以方便交流。 import java.util.Properties; import java.io.InputStream; import java.io.IOException; /** * 读取Properties文件的例子 * File: TestProperties.java * User: leimin * Date: 2008-2-15 18:38:40 */ public final class TestProperties { private static String param1; private static String param2; static { Properties prop = new Properties(); InputStream in = Object. class .getResourceAsStream( "/test.properties" ); try { prop.load(in); param1 = prop.getProperty( "initYears1" ).trim(); param2 = prop.getProperty( "initYears2" ).trim(); } catch (IOException e) { e.printStackTrace(); } } /** * 私有构造方法,不需要创建对象 */ private TestProperties() { } public static String getParam1() { return param1; } public static String getParam2() { return param2; } public static void main(String args[]){ System.out.println(getParam1()); System.out.println(getParam2()); } } 运行结果: 151 152 当然,把Object.class换成int.class也行。另外,如果是static方法或块中读取Properties文件,还有一种最保险的方法,就是这个类的本身名字来直接获取Class对象,比如本例中可写成TestProperties.class,这样做是最保险的方法。2 获取路径的方式:File fileB = new File( this .getClass().getResource( "" ).getPath()); System. out .println( "fileB path: " + fileB); 2.2获取当前类所在的工程名:System. out .println("user.dir path: " + System. getProperty ("user.dir"))<span style="background-color: white;">3 获取路径的一个简单的Java实现</span> /** *获取项目的相对路径下文件的绝对路径 * * @param parentDir *目标文件的父目录,例如说,工程的目录下,有lib与bin和conf目录,那么程序运行于lib or * bin,那么需要的配置文件却是conf里面,则需要找到该配置文件的绝对路径 * @param fileName *文件名 * @return一个绝对路径 */ public static String getPath(String parentDir, String fileName) { String path = null; String userdir = System.getProperty("user.dir"); String userdirName = new File(userdir).getName(); if (userdirName.equalsIgnoreCase("lib") || userdirName.equalsIgnoreCase("bin")) { File newf = new File(userdir); File newp = new File(newf.getParent()); if (fileName.trim().equals("")) { path = newp.getPath() + File.separator + parentDir; } else { path = newp.getPath() + File.separator + parentDir + File.separator + fileName; } } else { if (fileName.trim().equals("")) { path = userdir + File.separator + parentDir; } else { path = userdir + File.separator + parentDir + File.separator + fileName; } } return path; } 4 利用反射的方式获取路径:InputStream ips1 = Enumeration . class .getClassLoader() .getResourceAsStream( "cn/zhao/enumStudy/testPropertiesPath1.properties" ); InputStream ips2 = Enumeration . class .getResourceAsStream( "testPropertiesPath1.properties" ); InputStream ips3 = Enumeration . class .getResourceAsStream( "properties/testPropertiesPath2.properties" );

⑥ java怎么读取properties文件

/*** 用来测试获取配置文件的类* @author BaiKeyang**/public class ResourceUtils {public static String getValue(String file, String key) {        String value;        // 未取到值,从properites文件中查找        try {         ResourceBundle bundle = ResourceBundle.getBundle(file);            value = bundle.getString(key);            if (value==null){                throw new RuntimeException("没有该属性的值:"+key);            }            value = new String(value.getBytes("ISO-8859-1"), "UTF-8");            return value;        } catch (Throwable e) {         System.err.println(e);            return null;        }    }public static void main(String[] args) {String resourceFile = "config.dataBase";//      创建一个默认的ResourceBundle对象   //        ResourceBundle会查找包config下的dataBase.properties的文件   //        config是资源的包名,它跟普通java类的命名规则完全一样:   //        - 区分大小写   //        - 扩展名 .properties 省略。就像对于类可以省略掉 .class扩展名一样   //        - 资源文件必须位于指定包的路径之下(位于所指定的classpath中)   //        假如你是在非Web项目中使用,则一定要写资源文件的路径,也就是包路径必须存在。//        如果是Web项目,不写包路径可以,此时将资源文件放在WEB-INF\classes\目录下就可以。String url = getValue(resourceFile, "db.url");System.out.println(url);}}

⑦ 在java中如何读取properties文件

使用java.util.Properties1、创建一个Properties对象。2、使用对象的load方法加载你的property文件。3、使用getProperty方法取值回。例子:package com.bill.test;import java.io.FileInputStream;import java.util.Properties;public class Test {public static void main(String[] args) throws Exception{答Properties property = new Properties();property.load(new FileInputStream("你的文件位置"));String value = property.getProperty("你的属性的key");//TODO 使用value…}}

⑧ 在java中如何读取properties文件

使用java.util.Properties1、创建一个Properties对象。2、使用对象的load方法加载你的property文件专。3、使用getProperty方法取值。例子:属package com.bill.test;import java.io.FileInputStream;import java.util.Properties;public class Test {public static void main(String[] args) throws Exception{Properties property = new Properties();property.load(new FileInputStream("你的文件位置"));String value = property.getProperty("你的属性的key");//TODO 使用value…}}

未经允许不得转载:山九号 » java读取properties文件|在java中如何读取properties文件

赞 (0)