0

次のようなWindowsマシンからファイルディレクトリを読み込んでいます:

C:\\some dir\\someDir

を使用してこれをファイルに書き出すとjava.io.Writer.write()、どういうわけか\\文字が削除されますか? 私の出力は次のようになります。

C:some dirsomeDir

私の方法:

    /**
     * Sets the property value for given property.
     *     Allows for either overwriting the original file from what
     *     is in memory (default Java Properties API behavior) or perform
     *     an in-place update of a given property.
     * 
     * @param propertyName Name of property to be changed.
     * @param value Value to change named property to.
     * @param overwrite True to overwrite (replace) properties file with what is in memory 
     *             (no guarantee of property order and comments will be gone),
     *             False to update properties file (preserve comments and property order etc).
     * @return Return's true if successful operation, false if something failed.
     */
    public boolean setConfig(String propertyName, String value, boolean overwrite) {
        if (overwrite) {
            try {
                this.prop.load(new FileInputStream(this.propertyFile));
            } catch (IOException e) {
                return false;
            }
            this.prop.setProperty(propertyName, value);
            try {
                this.prop.store(new FileOutputStream(this.propertyFile), null);
            } catch (IOException e) {
                return false;
            }
            return true;
        } else {

            BufferedReader br = null;
            BufferedWriter bw = null;

            try {

                String curVal = this.getConfig(propertyName);

                FileInputStream fis = new FileInputStream(new File(this.propertyFile));
                br = new BufferedReader(new InputStreamReader(fis));

                String str = "";
                String line = br.readLine();
                while (line != null) {
                    str = str.concat(line.concat(System.getProperty("line.separator")));
                    line = br.readLine();
                }

                FileWriter fw = new FileWriter(new File(this.propertyFile));
                bw = new BufferedWriter(fw);

                bw.write(str.replace(propertyName + "=" + curVal, propertyName + "=" + value));

            } catch (IOException | ConfigException e) {
                return false;
            } finally {
                try {
                    br.close();
                    bw.close();
                } catch (IOException e) {
                    // does not matter
                }
            }
            return true;
        }
    }
4

0 に答える 0