2012-04-18 8 views
0
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") 

でも、パスは認識されないと言います。

+1

してくださいあなたのマシンのプロパティファイルとjavaファイルの場所の詳細を質問で与えてください。例:C:/properties/IxFbConfiguration.properties。 – sgowd

+0

'ls/properties/IxFbConfiguration.properties'や' dir c:\ properties \ IxFbConfiguration.properties'を実行できますか? –

+0

C:\\ドキュメントと設定\\ meenakshib.DCKAP-066 \\デスクトップ\\ xblitzjApril18 \\プロパティ\\ IxFbConfiguration.propertiesこれはFILEPATHです – Meenakshi

答えて

1

静的な値(パス)を使用して新しいファイル(...)を使用して、私見、あなたはこのコードは、あなたのIDEで作業したい場合は、ファイル

にアクセスするための正しい方法ではありません、とあなたにサーバ、および異なる環境のために、ファイルのパスは次のようになります。

、構成、および絶対パスのいずれか(C:/などのWindows ENV上)

または、クラスパスに

+0

しかし、getClass()。getResourceAsStream( "/ properties/IxFbConfiguration .properties ")は、jarファイルを使用するとパスとして認識されません。 – Meenakshi

+0

質問を再読み込みすると、このファイルを書きたいと思うので、クラスパスソリューションはうまく動作しません。 あなたのアプリケーションに必要なファイルの設定可能なパスが必要な場合があります(ただし、より多くの情報が必要な場合は、springを使用しますか、それともplain tomcatプロジェクトですか?) –

+0

これは純粋にJavaプロジェクトです - 私はフレームワークを使用していません。 – Meenakshi

関連する問題