導航:首頁 > 編程語言 > javaproperties讀取路徑

javaproperties讀取路徑

發布時間:2022-12-10 10:32:12

㈠ 在java中如何讀取properties文件

使用java.util.Propertiesx0dx0ax0dx0a1、創建一個Properties對象。x0dx0a2、使用對象的load方法載入你的property文件。x0dx0a3、使用getProperty方法取值。x0dx0a例子:x0dx0apackage com.bill.test;x0dx0ax0dx0aimport java.io.FileInputStream;x0dx0aimport java.util.Properties;x0dx0ax0dx0apublic class Test {x0dx0apublic static void main(String[] args) throws Exception{x0dx0aProperties property = new Properties();x0dx0aproperty.load(new FileInputStream("你的文件位置"));x0dx0aString value = property.getProperty("你的屬性的key");x0dx0a//TODO 使用value...x0dx0a}x0dx0a}

㈡ java讀取properties文件

InputStream in = getProperties.class.getClassLoader().getResourceAsStream(
"config.properties");
這一句換個寫法試試:

Properties props = new Properties();
String url = this.getClass().getClassLoader().getResource(
"config.properties").toString().substring(6);
String empUrl = url.replace("%20", " ");// 如果你的文件路徑中包含空格,是必定會報錯的
System.out.println(empUrl);
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(empUrl));
props.load(in);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}

我就是一直這么寫的,沒問題。
我猜你讀取文件為空的原因,是你的文件路徑有空格。

㈢ java中如何取得properties文件的路徑

首先你為什麼要重命名src,這個是放源碼的包,另外你之所以取不到是因為你當前路徑(classpath)下沒有src啊,src是上一層,你肯定找不到,

㈣ JAVA如何讀取指定路徑的properties文件 不是在工程下的

public static Properties p =null;
static{
p =System.getProperties() ;
try {
p.load(new FileInputStream(new File("D:\\PIC\\srv_cfg.properties")));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(p.getProperty("common.config"));
}

㈤ java讀取properties配置文件路徑問題

說說我的項目中的情況吧:
配置文件「weblogic11g.properties」保存在WEB-INFO目錄下,和web.xml在同一個目錄下。
一個JavaBean專門用於讀取配置文件的內容:

public class PropertiesIO {
private String fileName = null;

public PropertiesIO(String fileName){
this.fileName = getClass().getClassLoader().getResource("/").getPath() + "..\\" + fileName;
}

public String getValue(String key){
try{
InputStream in = new FileInputStream(fileName);
Properties prop = new Properties();
prop.load(in);
in.close();
return prop.getProperty(key);
}
catch(Exception err){
err.printStackTrace();
return null;
}
}
}

重點說明:getClass().getClassLoader().getResource("/")會得到當前項目下的「WEB-INF\classes」目錄,即JavaBean的*.class文件的根目錄,
getClass().getClassLoader().getResource("/").getPath() + "..\\" + fileName
就會得到當前項目下的「WEB-INF\weblogic11g.properties」文件。

getValue()是根據鍵值得到相應配置項的內容,這樣就簡單了。

㈥ 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 web工程中讀取properties文件,路徑一直不知道怎麼寫

1. 使用java.lang.Class類的getResourceAsStream(String name)方法

InputStreamin=getClass().getResourceAsStream("/config.properties");

在靜態方法中,由於不能使用getClass()方法,必須寫出類的名字。區別不大。

MyClass.class.getResourceAsStream("/config.properties");

使用這個方法,路徑前面可以加斜杠也可以不加。根據Class類getResourceAsStream()方法的JavaDoc:

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResourceAsStream.

Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:

If the name begins with a '/' ('u002f'), then the absolute name of the resource is the portion of the name following the '/'.

Otherwise, the absolute name is of the following form:

modified_package_name/name

Where the modified_package_name is the package name of this object with '/' substituted for '.' ('u002e').

就是說,這個path假如以斜杠開頭,則斜杠後面的部分是文件相對classpath的路徑;假如不是,Java會把這個path看作是「包名/文件名」的結構,會嘗試在這個類的包裡面去找,而不是從classpath開始找;在這種情況下,除非你把properties文件放到MyClass.class所屬的包裡面,不然都會是null的。

2. 使用java.lang.ClassLoader類的getResourceAsStream(String name)方法


路徑是不能加斜杠的!非常重要。

MyClass.class.getClassLoader().getResourceAsStream("config.properties");

這是因為使用classloader進行讀取,所輸入的參數必須是一個相對classpath的絕對路徑,在格式上,一個絕對路徑是不能以'/'開頭的。

注意這兩個方法是同名的,但路徑參數的格式截然不同。


3. 在Maven中的運用

現在幾乎所有的web project都是maven project,Maven的默認設置是把

src/main/resources/

加入到classpath裡面的。那麼,最好的做法是把你的properties文件放進src/main/resources裡面,然後用上面代碼讀取。用Class類的,一般要加斜杠;用ClassLoader類的,絕不能加斜杠!

假如是Eclipse裡面,需要把這個src/main/resources加到classpath裡面。具體操作是右擊工程,選擇「Configure buildpath」,根據Maven的要求,把src/main/java和src/main/resources都加進去,並且保證Exclude是none,Include是all,或者至少要包括你需要讀取的文件。

㈧ JAVA中如何讀取src下所有的properties文件

1.使用java.util.Properties類的load()方法


示例:

//文件在項目下。不是在包下!!

InputStream in = new BufferedInputStream(new FileInputStream("demo.properties")) ;

Properties p = new Properties();

p.load(in) ;

String className2 = p.getProperty("database.driver");

String url = p.getProperty("database.url");

String user = p.getProperty("database.user");

String password = p.getProperty("database.pass");

總結:如果是 在WEB上讀取properties文件,寫成下面這種。上面寫的那些只在 JavaSE 中

String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();

System.out.println(path);

InputStream in = new FileInputStream(new File(path+File.separator+"mysql.properties"));

Properties prop = new Properties();

㈨ java中怎麼讀取properties

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class PropertiesTest
{
/*定義靜態方法,類名可以直接調用,用於讀取properties屬性文件中的內容*/
public static void initFile()
{
/*D://a.properties源屬性文件的路徑和名字*/
File file = new File("D://a.properties");
FileInputStream fis = null;
try
{
/*輸入流和屬性文件關聯*/
fis = new FileInputStream(file);
/*創建屬性集對象*/
Properties prop = new Properties();
/*將讀取的內容載入到屬性集對象中*/
prop.load(fis);
/*返回屬性列表中所有鍵的枚舉*/
Enumeration enums = prop.propertyNames();
while (enums.hasMoreElements())
{
/*將每一條屬性強制轉換為String類型,得到鍵key*/
String key = (String) enums.nextElement();
/*根據鍵得到對應的值value(String類型)*/
String value = prop.getProperty(key);
/*輸出properties屬性文件的內容*/
System.out.println(key+"----"+value);
}
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
if(fis!=null)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
/*調用方法讀取屬性文件中內容*/
PropertiesTest.initFile();
}
}

結果是:
UserPass----
URl----jdbc:microsoft:sqlserver://localhost:1433;databasename=employee
Driver----com.microsoft.jdbc.sqlserver.SQLServerDriver
UserName----sa

㈩ java讀取properties文件

個人建議使用FileInputStrean流 你試試看。因為文件流載入文件應該沒問題。
InputStream in = getProperties.class.getClassLoad().getResourceAsStream(
"config.properties");這句改成FileInputStream in=new FileInputStream("/config.properties")看看。

閱讀全文

與javaproperties讀取路徑相關的資料

熱點內容
如何批量快速壓縮視頻 瀏覽:432
我的世界如何加入ice伺服器 瀏覽:873
兄弟cnc編程說明書 瀏覽:204
php閃電入門教程學習 瀏覽:152
金岳霖邏輯pdf 瀏覽:938
linuxtomcat線程 瀏覽:77
pboc長度加數據加密 瀏覽:187
英雄聯盟國際服手游怎麼下安卓 瀏覽:297
程序員的思路 瀏覽:234
只能用命令獲得的四種方塊 瀏覽:358
怎麼用命令方塊防止開創造 瀏覽:807
掃描版的pdf 瀏覽:790
編程貓怎樣做3d游戲 瀏覽:207
怎麼查找雲伺服器上的ftp 瀏覽:156
我的世界伺服器如何注冊賬號 瀏覽:934
統計英文字元python 瀏覽:423
linux信息安全 瀏覽:908
壓縮機接線柱爆 瀏覽:1000
程序員自主創業 瀏覽:584
匯編程序員待遇 瀏覽:359