读取properties文件

武晨伟的博客
http://blog.csdn.net/bobor_2008/archive/2008/11/05/3225918.aspx

上看了一段代码还不错,稍加修改,粘贴如下,备用。

package com.test;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

/**
 * Util class that will read properties from the WEB-INF/classes/directory or by
 * specifying a URL on the filesystem. Also has a helper method for creating a
 * platform independent URL.
 */
public class PropertyReader {

	/**
	 * Retrieve the properties specified by the fileName The property file
	 * should be in the WEB-INF/classess directory Suppose you need to get the
	 * properties in the web-inf/classes/config/application.properties , you
	 * need to pass the propertyFile: config/application.properties
	 * 
	 * @param propertyFile
	 *            relative path to a properties file in the WEB-INF/classes
	 *            directory
	 * @return a <code>Properties<code> object based on the input file
	 **/
	public static Properties getProperties(String propertyFile) {
		try {
			URL url = getPropertiesURL(propertyFile);
			return getProperties(url);
		} catch (Exception e) {
			System.out.println("Error ocurred during properties retrieval");
			System.out.println(e.getMessage());
			return null;
		}
	}

	/**
	 * This method will return a platform independent URL to a file in the
	 * web-inf/classes direcotry.
	 * 
	 * @param fileName
	 *            relative path to a properties file in the WEB-INF/classes
	 *            directory
	 * @return a platform independent URL to the xml file.
	 */
	public static URL getPropertiesURL(String fileName) {
		try {
			System.out.println("Getting the properties URL");
			URL url = null;
			url = PropertyReader.class.getResource("/" + fileName);
			String s = url.toString();
			System.out.println("Filename of the  properties file is: " + s);
			if (s.indexOf("file://") != -1) {
				int indexOf = s.indexOf("file://") + 6;
				String temp = s.substring(0, indexOf);
				System.out.println("temp = " + temp + " moet zijn file:/");
				url = new URL(temp + "//" + s.substring(indexOf));
				System.out.println("The url is now: " + url);
			}
			return url;
		} catch (Exception e) {
			System.out.println("Error ocurred during properties retrieval");
			System.out.println(e.getMessage());
			return null;
		}
	}

	/**
	 * This method will return a platform independent URL to a file in the
	 * web-inf/classes/"packgageName" direcotry.
	 * 
	 * @param fileName
	 *            relative path to a properties file in the
	 *            web-inf/classes/"packgageName" directory
	 * @return a platform independent URL to the xml file.
	 */
	public static URL getPropertiesPackagedURL(String fileName) {
		try {
			System.out.println("Getting the properties URL");
			URL url = null;
			url = PropertyReader.class.getResource(fileName);
			String s = url.toString();
			System.out.println("Filename of the  properties file is: " + s);
			if (s.indexOf("file://") != -1) {
				int indexOf = s.indexOf("file://") + 6;
				String temp = s.substring(0, indexOf);
				System.out.println("temp = " + temp + " moet zijn file:/");
				url = new URL(temp + "//" + s.substring(indexOf));
				System.out.println("The url is now: " + url);
			}
			return url;
		} catch (Exception e) {
			System.out.println("Error ocurred during properties retrieval");
			System.out.println(e.getMessage());
			return null;
		}
	}

	/**
	 * Retrieve the properties accesible through the specified URL
	 * 
	 * @param url
	 *            a reference to a properties file
	 * @return a properties file
	 **/
	public static Properties getProperties(URL url) {
		try {
			Properties props = new Properties();
			// Check for Solaris compatibility.
			// A // in the file protocol won't be found in Solaris.
			props.load(url.openStream());
			System.out.println("Properties have been loaded: " + props);
			return props;
		} catch (Exception e) {
			System.out.println("Error ocurred during properties retrieval");
			System.out.println(e.getMessage());
			return null;
		}
	}

	public static void main(String[] args) {
		// 文件位于src下
		PropertyReader.getProperties("abc.properties");

		// 文件位于src/packageurl下
		URL url = PropertyReader.getPropertiesPackagedURL("abc.properties");
		PropertyReader.getProperties(url);

		// 文件位于项目根目录下
		File file = new File("abc.properties");
		try {
			PropertyReader.getProperties(file.toURL());
		} catch (MalformedURLException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
	}
}
package com.test;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;


public class PropertyFileReadTest {
	public static void main(String[] args) {
		TestReadPropertiesFile1();
		TestReadPropertiesFile2();
		TestReadPropertiesFile3();
		TestReadPropertiesFile4();
		TestReadPropertiesFile5();
	}
	/**
	 * 使用java.util.Properties类的load()方法
	 */
	public static Properties readPropertiesFile1(String fileName) throws IOException {
		InputStream in;
		in = new BufferedInputStream(new FileInputStream(fileName));
		Properties p = new Properties();
		p.load(in);
		return p;
	}
	public static void TestReadPropertiesFile1(){
		String filename="quartz.properties";
		try {
			readPropertiesFile1(filename);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (Exception e) {
		}
	}
	
	/**
	 * 使用java.util.ResourceBundle类的getBundle()方法
	 * 注意:该方法需要将该文件添到构建路径中
	 */
	public static ResourceBundle readPropertiesFile2(String fileName) {
		ResourceBundle rb=ResourceBundle.getBundle(fileName,Locale.getDefault());
		
		return rb;
	}
	public static void TestReadPropertiesFile2() {
		String filename = "quartz";
		ResourceBundle rb = readPropertiesFile2(filename);
		System.out.println(rb.getString("org.quartz.scheduler.instanceId"));
	}
	
	/**
	 * 使用java.util.PropertyResourceBundle类的构造函数:
	 */
	public static ResourceBundle readPropertiesFile3(String fileName)throws IOException {
		InputStream in = new BufferedInputStream(new FileInputStream(fileName));
		ResourceBundle rb = new PropertyResourceBundle(in);
		return rb;
	}
	public static void TestReadPropertiesFile3() {
		String filename = "quartz.properties";
		try {
			ResourceBundle rb;
			rb = readPropertiesFile3(filename);
			System.out.println(rb.getString("org.quartz.scheduler.instanceId"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 使用class变量的getResourceAsStream()方法
	 */
	public static Properties readPropertiesFile4(String fileName)throws IOException {
		InputStream in= PropertyFileReadTest.class.getResourceAsStream(fileName);
		Properties p=new Properties();
		p.load(in);
		return p;
	}
	public static void TestReadPropertiesFile4() {
		String filename="quartz.properties";
		try {
			Properties pro = readPropertiesFile4(filename);
			System.out.println(pro);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (Exception e) {
		}
	}
	/**
	 * 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
	 * @throws IOException 
	 */
	public static Properties readPropertiesFile5(String fileName) throws IOException {
		InputStream in=ClassLoader.getSystemResourceAsStream(fileName);
		Properties p=new Properties();
		p.load(in);
		return p;
	}
	public static void TestReadPropertiesFile5() {
		String filename="quartz.properties";
		try {
			Properties pro = readPropertiesFile5(filename);
			System.out.println(pro);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}catch (Exception e) {
		}
	}
}

文件操作:将文件按照一行一行读取,读取为list

 

/**
 * 将文件按“行”读取为list
 * @author apple
 */
public class PropertyFileReadTest {
	public static void main(String[] args) {
		String filename = "quartz.properties";
		try {
			List list = readFileToArrayList(filename);
			for (Iterator iter = list.iterator(); iter.hasNext();) {
				System.out.println(iter.next());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 将文件按“行”读取为list
	 * @param filename
	 * @return list
	 * @throws IOException
	 */
	public static List readFileToArrayList(String filename) throws IOException {
		List list = new ArrayList();
		FileInputStream fin = new FileInputStream(filename);
		InputStreamReader read = new InputStreamReader(fin, "UTF-8");// 编码处理
		BufferedReader br = new BufferedReader(read);
		String line = br.readLine();// 从文件读取一行字符串
		while (line != null) {
			list.add(line);
			line = br.readLine();// 从文件中继续读取一行数据
		}
		br.close();// 关闭BufferedReader对象
		read.close();// 关闭文件
		fin.close();
		return list;
	}
}

1。使用java.util.Properties类的load()方法
示例:
InputStream in = lnew BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);

2。使用java.util.ResourceBundle类的getBundle()方法
示例:
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

3。使用java.util.PropertyResourceBundle类的构造函数
示例:
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

4。使用class变量的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
示例:
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
示例:
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

补充

Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
示例:
InputStream in = context.getResourceAsStream(path);
Properties p = new Properties();
p.load(in);

你可能感兴趣的:(java,apple,Web,quartz,Solaris)