在Java中将特殊字符写入属性文件的问题

huangapple go评论111阅读模式
英文:

Problem in writing special characters to a properties file in Java

问题

我需要向属性文件中添加键值对。
除了#和=之外,其他都正常工作,每次在字符之前添加\。

请给我一些建议。

当前属性文件数据

paper = Normalised

我想评论这个键

#paper = Normalised

但现在发生的是\被添加了

\#paper = Normalised

'''

      String valueOfKey = updatedMap.get(key);

      updatedMap.remove(key);

      updatedMap.put("#" + key, valueOfKey);
    
   String totalPath = propertiesService.getFilePath(request) + "\\" + propertiesModel.getSelectedFile();

      propertiesService.updatePropertyfile(updatedMap, request, totalPath);

'''

'''

public boolean updatePropertyfile(Map<String, String> map, HttpServletRequest request, String fileName) {
      Properties props = new Properties();
Writer Out = null;
      File file = new File(fileName);
try {
        FileOutputStream out = new FileOutputStream(file);
        Out = new BufferedWriter(new OutputStreamWriter(out));
        Set<String> keyset = map.keySet();
        Iterator iter = keyset.iterator();
        while (iter.hasNext()) {
          String key = (String) iter.next();
          props.setProperty(key, (String) map.get(key));
   }
        props.store(Out, "update");
        Out.flush();
        Out.close();
      } catch (IOException e) {
        return false;
      }
 return true;
    }

'''

在属性文件中写入的值

  • \#paper = Normalised
英文:

I need to add key value pairs to a properties file.
All are working fine except # and = everytine a \ is appended before the characters.

Please share me any suggestion.

current properties file data

paper = Normalised

I want to comment this key

#paper = Normalised

but what is happening is \ is getting added

\#paper = Normalised

'''

      String valueOfKey = updatedMap.get(key);

      updatedMap.remove(key);

      updatedMap.put(&quot;#&quot; + key, valueOfKey);
    
   String totalPath = propertiesService.getFilePath(request) + &quot;\\&quot; + propertiesModel.getSelectedFile();

      propertiesService.updatePropertyfile(updatedMap, request, totalPath);

'''

'''

public boolean updatePropertyfile(Map&lt;String, String&gt; map, HttpServletRequest request, String fileName) {
      Properties props = new Properties();
Writer Out = null;
      File file = new File(fileName);
try {
        FileOutputStream out = new FileOutputStream(file);
        Out = new BufferedWriter(new OutputStreamWriter(out));
        Set&lt;String&gt; keyset = map.keySet();
        Iterator iter = keyset.iterator();
        while (iter.hasNext()) {
          String key = (String) iter.next();
          props.setProperty(key, (String) map.get(key));
   }
        props.store(Out, &quot;update&quot;);
        Out.flush();
        Out.close();
      } catch (IOException e) {
        return false;
      }
 return true;
    }

'''

Value in property file getting written

  • \#paper = Normalised

答案1

得分: 1

哈希标签是Java属性文件中评论的引导符号:

#由生成器在2020-05-01创建
#当前属性文件数据
paper = Normalised

#想要更新,类似于
#paper = Normalised – 但这是一个评论…

#但现实情况是会添加\#
\#paper = Normalised#需要反斜杠…

因此,通过反斜杠进行转义是使其正常工作的唯一方法。

不幸的是,当您需要使用java.util.Properties以外的另一个API读取属性文件时,您必须将此功能添加到解析器中。

英文:

The hash tag is the lead-in for a comment in Java Properties files:

# Created by generator on 2020-05-01
#current properties file data
paper = Normalised

#want to update like
#paper = Normalised – but this is a comment …

#but what is happening is \# is getting added
\#paper = Normalised # Backslash required …

So the escape with the backslash is the only way to get it working.

Unfortunately, when you need to read the Properties file with another API than java.util.Properties, you have to add this capability to your parser.

huangapple
  • 本文由 发表于 2020年5月2日 16:19:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61556378.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定