簡體   English   中英

讀取屬性文件時找不到文件異常

[英]File not found exception while reading a property file

 public boolean  WriteProperty(String key,String value){
            try{  
                    int check=0;
                    while(check == 0){
                check=1;

                Properties pro = new Properties();

                File f = new File("/properties/IxFbConfiguration.properties");
                if(!f.exists()){
                  check=0;
                  System.out.println("File not found!");
                }
                else{
                  FileInputStream in = new FileInputStream(f);
                  pro.load(in);
                  System.out.print("Enter Key : ");
                  System.out.print("Enter Value : ");
                  pro.setProperty(key, value);

                  System.out.println("the property is"+pro.getProperty(key));
                 // pro.store(new FileOutputStream(str + ".properties"),null);
                  pro.store(new FileOutputStream("/properties/IxFbConfiguration.properties"),null);
                  System.out.println("Operation completly successfuly!");
                }
              }
            }
            catch(IOException e){
            System.out.println(e.getMessage());
            }
            return false;
          }

運行此代碼時,我找不到文件異常。

我確實有一個包含IxFbConfiguration.properties文件的文件夾屬性。 當我將完整路徑硬編碼為C:\\ Documents and Settings \\ meenakshib.DCKAP-066 \\ Desktop \\ xblitzjApril18 \\ properties \\ IxFbConfiguration.properties時,它可以工作。

但是我在使用廣口瓶時遇到了問題。 我嘗試使用

 getClass().getResourceAsStream("/properties/IxFbConfiguration.properties")

也有,但它說的路徑未被認可。

使用帶有靜態值(路徑)的新File(....)IMHO,不是訪問文件的正確方法

如果您希望此代碼在IDE,服務器上以及針對不同環境的環境下工作,則文件的路徑應為:

可以配置,也可以是絕對路徑(在Windows env上為C:/等)。

或者,在類路徑中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM