1

I am setting some string content using prop.put function and writing it to a .txt file.

Properties prop = new Properties();
OutputStream output = null;

String uName = "Name=Anand, Age=25";
prop.put("User", uName);

output = new FileOutputStream("src/main/resources/node.txt");
prop.store(output, null);

When I open the node.txt, it is written as User=Name\=Anand, Age\=25. How to write the same to a file without \. i.e., User="Name=Anand, Age=25". Can someone help me here ?

Muruga Balu
  • 115
  • 10
  • 3
    You might not want to do this, the \ is there to act as an escape character so that it knows not to process `=` as what it otherwise would, and to treat it instead as the actual character. – adickinson Mar 29 '19 at 10:05
  • 3
    Possible duplicate of [How to escape the equals sign in properties files](https://stackoverflow.com/questions/2406975/how-to-escape-the-equals-sign-in-properties-files) – Jean-Baptiste Yunès Mar 29 '19 at 10:06
  • https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#store(java.io.Writer,%20java.lang.String) `Then every entry in this Properties table is written out, one per line. For each entry the key string is written, then an ASCII =, then the associated element string. For the key, all space characters are written with a preceding \ character. For the element, leading space characters, but not embedded or trailing space characters, are written with a preceding \ character.` – Vüsal Mar 29 '19 at 10:08
  • @Vusal That quote doesn't really make sense because it is about space characters, did you intend to quote _"The key and element characters #, !, =, and : are written with a preceding backslash to ensure that they are properly loaded."_ instead? – Mark Rotteveel Mar 29 '19 at 10:59

2 Answers2

0

Try this:

String uName = "\"Name\u003dAnand, Age\u003d25\"";
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • This is actually the same as writing `String uName = "\"Name=Anand, Age=25\"";` due to [lexical translation](https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.2). – MC Emperor Mar 29 '19 at 10:47
0

The Properties class can't support this.
How about overvide the method of Properties, store?
To implement the function(Properties.saveConvert) yourself!
You can remove the code "case '=':", but it only support you to write;

      switch(aChar) {
            case ' ':
                if (x == 0 || escapeSpace)
                    outBuffer.append('\\');
                outBuffer.append(' ');
                break;
            case '\t':outBuffer.append('\\'); outBuffer.append('t');
                      break;
            case '\n':outBuffer.append('\\'); outBuffer.append('n');
                      break;
            case '\r':outBuffer.append('\\'); outBuffer.append('r');
                      break;
            case '\f':outBuffer.append('\\'); outBuffer.append('f');
                      break;
            case '=': // What you need do is to remove this line
            case ':': // Fall through
            case '#': // Fall through
            case '!':
                outBuffer.append('\\'); outBuffer.append(aChar);
                break;
            default:
                if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
                    outBuffer.append('\\');
                    outBuffer.append('u');
                    outBuffer.append(toHex((aChar >> 12) & 0xF));
                    outBuffer.append(toHex((aChar >>  8) & 0xF));
                    outBuffer.append(toHex((aChar >>  4) & 0xF));
                    outBuffer.append(toHex( aChar        & 0xF));
                } else {
                    outBuffer.append(aChar);
                }
        }
Shine
  • 1
  • 1