1. properties文件怎么写
InputStream in = 类名.class.getClassLoader().getResourceAsStream("propertes名字.properties");
Properties prop = new Properties();
prop.load(in)
oracleDb_Driver = prop.getProperty("oracleDb_Driver-properties里面的字段");
2. java的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: leizhimin * 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"))3 获取路径的一个简单的Java实现 /** *获取项目的相对路径下文件的绝对路径 * * @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" );。
3. eclipse properties怎么写
资源文件 即 .properties 文件是常用于国际化;
eclipse默认的 .properties 文件编辑器有几个问题:
编码问题
多种语言同步问题
下面介绍2种eclipse的 .properties 文件的插件,可以方便的编辑 .properties 文件
(1) PropEdit
PropEdit,可以解决 编码问题,也就是说它可以自动将各种语言转化成所需的编码格式
安装方法(更新方式):
利用eclipse的更新功能,地址为 htt p:/ /propedit.sourceforg e.j p/eclipse/updates/
注意:最新的 propedit 不支持 myeclipse6.5 ,必须 下载旧版的进行安装;
htt p:/ /sourceforge.j p/projects/propedit/downloads/36845/jp.gr.ja va_conf.ussiy.app.propedit_5.0.1.zip
(2)JInto
JInto 可以同时解决 编码问题 和 多种语言同步问题
安装方法(link方式):
* 下载包,地址:htt p:/ /ww w.guh-software.de/jinto_e n.h tml
* 下面以安装在 d:/myeclipse6.5/ext-link/ 目录下为例
* 首先在 d:/myeclipse6.5/ext-link/ 下建立一个目录,名为 “jinto”
* 解压 下载的包,可以看到包下有 “eclipse”目录,“eclipse”目录下又有“plugins”和“features”目录;
* 将 “eclipse”目录放到“jinto”目录下
* 新建1个link文件,名为 jinto.link ,放在 eclipse安装的根目录下的 link文件夹里;
* jinto.link 里写1行内容,即 “path=d:/myeclipse6.5/ext-link/jinto”
* 重启 eclipse或myeclipse
(3)设置默认的 .properties文件编辑器
eclipse中, window->preferences->General->Editors->File Asssociations ,找到 .properties ,选中相应的编辑器,点击 “default”按钮;其中 “jinto”对应的编辑器名是“Java ResourceBundle Editor” ,“ PropEdit”对应的编辑器名是“PropertiesEditor”
4. 如何写一个.properties文件,如何调用
properties属性文件内容都是以键值对形式存在的,比如我写一个叫test.properties的文件,打开后可以再里面写如:name=Tom
而在java类中需要new一个Properties类的对象,如下:
Properties properties = new Properties();
接下来需要获取test.properties的文件路径:
String path = Thread.currentThread().getContextClassLoader().getResource("test.properties").getPath();
然后加载该文件:
properties.load(new FileInputStream(path));
最后你就可以get它的属性了:
String name_1=properties.getProperty("name");
这个name_1的值就是“TOM”了。
(因为涉及到文件流,所以加载那一步需要try catch,根据编译器提示自己加吧)
5. properties文件怎么写
InputStream in = 类名.class.getClassLoader().getResourceAsStream("propertes名字.properties");
Properties prop = new Properties();
prop.load(in)
oracleDb_Driver = prop.getProperty("oracleDb_Driver-properties里面的字段");
6. java怎么读取properties文件怎么写
InputStream in = 类名.class.getClassLoader().getResourceAsStream("propertes名字.properties");
Properties prop = new Properties();
prop.load(in)
oracleDb_Driver = prop.getProperty("oracleDb_Driver-properties里面的字段");
7. sqlserver的properties怎么写
连接 URL 的一般形式为:
jdbc:sqlserver://[serverName[\instanceName][:portNumber]][;property=value[;property=value]]
jdbc:sqlserver://(必需)称为子协议,且为常数。
serverName(可选)是要连接到的服务器的地址。它可以是 DNS 或 IP 地址,也可以是本地计算机地址 localhost 或 127.0.0.1。如果未在连接 URL 中指定服务器名称,则必须在属性集中指定。
instanceName (可选)是 serverName 上要连接到的实例。如果未指定,则会连接到默认实例。
portNumber(可选)是 serverName 上要连接到的端口。默认值为 1433。如果使用默认端口,则无需在 URL 中指定端口及其前面的“:”。 注意:若要获得最佳连接性能,应在连接到指定实例时设置 portNumber。这将避免为了确定端口号而与服务器进行往返通讯。如果同时使用 portNumber 和 instanceName,则会优先使用 portNumber,而忽略 instanceName。
property(可选)是一个或多个选项连接属性。有关详细信息,请参阅JDBC 连接 SQL Server 数据库 - 设置连接属性 。可指定该列表中的任何属性。属性只能用分号(“;”)分隔,且不允许重复。
连接实例使用用户名和密码连接到指定数据库:
jdbc:sqlserver://localhost:1433;databaseName=AdventureWorks;integratedSecurity=true
;user=MyUserName;password=*****;
8. Java中的properties配置文件怎么写,代码
public static void main(String[] args) {
Properties p = new Properties();
p.setProperty("id", "user1");
p.setProperty("password", "123456");
try{
PrintStream stm = new PrintStream(new File("e:\test.properties"));
p.list(stm);
} catch (IOException e) {
e.printStackTrace();
}
}
9. 怎样在.properties文件中写中文呢
properties文件是这么写的吗。。,应该是key=value行式的吧。注释则是开头用'#'井号
比如
### valid values are: true, false (true is the default)
struts.objectFactory.spring.useClassCache = true
另外,在属性文件中是不能写入中文的,即使写入了中文,读出来的也是乱码(注释除外,注释是给人看的,不是让程序来读的)。而你之所以写进去的中文自动转成了Unicode编码,可能是用eclipse的properties editor的添加编辑界面添加导致的(如下图),该界面本来就是增加属性文件的属性用的。如果是要加注释,需点击下面的source标签,切换到文本编辑模式,在要加注释的项之前插入一行,首字符为'#',然后输入你的中文注释即可
转载请注明出处育才学习网 » properties怎么写