6种读取属性文件的JAVA实现方式?请高手给讲讲。

Shangliuxiaozhu 2009-07-20 10:11:57
package cn.liwenjun.sfile;

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 readfile {

/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String name="C:\\bsmain_runtime.log";//随便一个文件想在控制台上打印这个文件的内容
// readfile1(name);
// readfile2(name);
// readfile3(name);
// readfile4(name);
// readfile5(name);
readfile6(name);
}
public static void readfile1(String name) throws IOException{
//1.使用java.util.Properties类的load()方法
System.out.print("第1个方法");
InputStream in = new BufferedInputStream(new FileInputStream(name));
Properties p = new Properties();
p.load(in);
System.out.print(p);
}
public static void readfile2(String name) throws IOException{
//2.使用java.util.ResourceBundle类的getBundle()方法
System.out.print("第2个方法");
ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault());

}
public static void readfile3(String name) throws IOException{
//3.使用java.util.PropertyResourceBundle类的构造函数 
System.out.print("第3个方法");
InputStream in = new BufferedInputStream(new FileInputStream(name));
ResourceBundle rb = new PropertyResourceBundle(in);

}
public static void readfile4(String name) throws IOException{
//4.使用class变量的getResourceAsStream()方法
System.out.print("第4个方法");
InputStream in = JProperties.class.getResourceAsStream(name);
Properties p = new Properties();
p.load(in);


}
public static void readfile5(String name) throws IOException{
//5.使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法 
System.out.print("第5个方法");
InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name);
Properties p = new Properties();
p.load(in);

}
public static void readfile6(String name) throws IOException{
//6.使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法 
System.out.print("第6个方法");
InputStream in = ClassLoader.getSystemResourceAsStream(name);
Properties p = new Properties();
p.load(in);

}

}
...全文
66 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
closewbq 2009-07-20
  • 打赏
  • 举报
回复
恩挺好。
彷徨的蜗牛 2009-07-20
  • 打赏
  • 举报
回复
以前还不晓得,原来有这么多的方法实现,学习一下,帮忙顶顶
GGMMYQL 2009-07-20
  • 打赏
  • 举报
回复
呵呵,看看
Shangliuxiaozhu 2009-07-20
  • 打赏
  • 举报
回复
有的方法不知道,怎样在控制台上打印。第一个可以,但是其他就不知道了?请高手给讲讲。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧