0

私は以下の値を持つ1つのプロパティファイル(ab.properties)があります。値はプロパティファイルで正しく設定され取得されていません

色=オレンジ

storeLocation =/test.json

会社を=リンゴ

私はCにstoreLocationの値を変更したい:\ Users \ユーザークマー\ TESTFILES \コードで

test.json、ファイルは、私は私のab.propertを読んでいた場所からのパスですファイルとstorelocation1にはC:\ Users \ kumar \ testFiles \ test.jsonというパスが含まれています(storelocationで更新したい)。コードの下に以下を参照してください

try (InputStream in = new FileInputStream(file)) { 
      Properties prop = new Properties(); 
      prop.load(in); 
      in.close(); 
      prop.setProperty("storeLocation", storeLocation1); 
      OutputStream out = new FileOutputStream(file); 
      prop.store(out, null); 
      out.close(); 


     }catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

上記のコードを使用することによって、私は以下の出力取得しています:

色=オレンジ

storeLocation = C \:\\ユーザー\\クマー\\ TESTFILES \\をtest.json

storeLocation用=リンゴ

値を更新取得している会社が、

私はCがほしいです:Cではありません。誰も私にこれを導くことができますか?

+0

「私はCをしたい:いないC:」?どういう意味ですか? – Ofisora

+0

これは、このクエリを投稿したときにバックスラッシュ文字が正しく取得されないようです。私は出力を –

+0

storeLocation = C \:\\ Users \\ kumar \\ testFiles \\ test.json –

答えて

0

あなたは準備ができてプロパティファイルに次のアプローチを使用することができます:あなたはPropertiesで、ファイルで、この場合は

public static Properties store(File file, Properties properties) throws IOException { 
    try (BufferedWriter out = new BufferedWriter(new FileWriter(file, false))) { 
     Enumeration<String> names = (Enumeration<String>)properties.propertyNames(); 

     while (names.hasMoreElements()) { 
      String key = names.nextElement(); 
      String val = properties.getProperty(key); 
      out.write(String.format("%s = %s", key, val)); 
      out.newLine(); 
     } 
    } 

    return properties; 
} 

GEまったく同じ文字列を::

public static Properties load(File file) throws IOException { 
    try (BufferedReader in = new BufferedReader(new FileReader(file))) { 
     int pos; 
     String line; 
     Properties properties = new Properties(); 

     while ((line = in.readLine()) != null) { 
      if (!line.startsWith("#")) { 
       if ((pos = line.indexOf('=')) > 0) { 
        String key = line.substring(0, pos).trim(); 
        String val = line.substring(pos + 1).trim(); 
        properties.setProperty(key, val); 
       } 
      } 
     } 

     return properties; 
    } 
} 

とプロパティファイルを保存するC:\ Users \ kumar \ testFiles \ test.json。 私はエスケープ記号でどのように動作するのかわかりませんが、この例ではあなたとアイデアを与えるかもしれません。実生活で

私はそのような要求があった場合、私はBuffereReaderBufferedWriterをオーバーライドして、標準Properties.load()Properties.store()メソッドを使用します。当面のために

+0

私はあなたのコードを使用しましたが、私はまだ同じ出力を得ています。すなわちC \:\\ Users \\ kumar \\ testFiles \\ test.json –

0

は、私は今のように私のために機能するソリューション、下に使用しています:

try (InputStream in = new FileInputStream(file)) { 
     properties prop = new Properties(); 
     prop.load(in); 
     in.close(); 
     prop.setProperty("storeLocation", storeLocation1); 
     OutputStream out = new FileOutputStream(file); 
     prop.store(out, null); 
     out.close(); 


    }catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    updateString(file); 
} 

    public static void updateString(File file) throws IOException { 
     Path path = Paths.get(file.toString()); 
     Charset charset = StandardCharsets.UTF_8; 

     String content = new String(Files.readAllBytes(path), charset); 
     content = content.replace("\\" + ":", ":"); 
     Files.write(path, content.getBytes(charset)); 

    } 
関連する問題