Discussions
Categories
- 196.9K All Categories
- 2.2K Data
- 239 Big Data Appliance
- 1.9K Data Science
- 450.3K Databases
- 221.7K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 550 MySQL Community Space
- 478 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3K ORDS, SODA & JSON in the Database
- 546 SQLcl
- 4K SQL Developer Data Modeler
- 187K SQL & PL/SQL
- 21.3K SQL Developer
- 295.9K Development
- 17 Developer Projects
- 138 Programming Languages
- 292.6K Development Tools
- 107 DevOps
- 3.1K QA/Testing
- 646K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 155 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.1K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 18 Java Essentials
- 160 Java 8 Questions
- 86K Java Programming
- 80 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 204 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 442 LiveLabs
- 38 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 171 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 232 Portuguese
backslash added while reading File

Hello,
I am reading a file:
FileReader fr = null;BufferedReader br = null;fr = new FileReader(this.resource.toString());br = new BufferedReader(fr);String sCurrentLine;String fullFile = "";while (null != (sCurrentLine = br.readLine())) { System.out.println("line" + sCurrentLine); fullFile += sCurrentLine + "\n";}
The original file content is:
#Tue Feb 20 08:40:24 WET 2018DataEngineFactory=dummy.app.EngineFactoryPersistCache=falseLoginLocale=en_GBbrandServiceRoot=https://nextgen-wlws.conceptsoftware.eu:9075/SessionTimeout=3600Debug=trueCustProvider=trueCacheSize=1000
But in console log it is printing:
If you see the ServiceRoot property (which is supposed to be an URL) it added backslashes before ':' (do not mind the LoginLocale property, it was deliberately changed to teste).
Why is that happening.
I thought it could be the BufferedReader class, but i tried with others too and all give same output:
//with scannerScanner sc = new Scanner(this.resource);while (sc.hasNextLine()) { sCurrentLine = sc.nextLine(); System.out.println("scanner" + sCurrentLine); fullFile += sCurrentLine + "\n";}//with apache commonsfullFile = FileUtils.readFileToString(new File(this.resource.toString()));
Can you help me?
Thanks
Carlos
Answers
-
Can you help me?
No - at least not until you post the ACTUAL code you are using and the output you are getting from that code.
SHOW US:
1. WHAT you do
2. HOW you do it
3. WHAT results you get
The code you posted isn't complete and the 'file contents' you posted do NOT match what you say that code produces.
FileReader fr = null; BufferedReader br = null; fr = new FileReader(this.resource.toString()); br = new BufferedReader(fr); String sCurrentLine; String fullFile = ""; while (null != (sCurrentLine = br.readLine())) { System.out.println("line" + sCurrentLine); fullFile += sCurrentLine + "\n"; }
The original file content is:
- #TueFeb2008:40:24WET2018
- DataEngineFactory=dummy.app.EngineFactory
- PersistCache=false
- LoginLocale=en_GBbrand
- ServiceRoot=https://nextgen-wlws.conceptsoftware.eu:9075/
- SessionTimeout=3600
- Debug=true
- CustProvider=true
- CacheSize=1000
#Tue Feb 20 08:40:24 WET 2018 DataEngineFactory=dummy.app.EngineFactory PersistCache=false LoginLocale=en_GBbrand ServiceRoot=https://nextgen-wlws.conceptsoftware.eu:9075/ SessionTimeout=3600 Debug=true CustProvider=true CacheSize=1000
But in console log it is printing:
See code line #1 above? Where it says '#TueFeb2008:40:24:24WET2018'?
See the first console output line above? Where it says "#Tue Feb 20 09:32:31 WET 2018'?
Those dates do NOT match and could NOT have been produced with the code, or file, you posted.
The code also won't compile since it is incomplete - so we have no way of trying to reproduce what you report.
-
Hello,
Thanks for response.
So i am going to clarify you.
The original file content is/was INDEED what i posted and you copy/paste:
#TueFeb2008:40:24WET2018DataEngineFactory=dummy.app.EngineFactoryPersistCache=falseLoginLocale=en_GBbrandServiceRoot=https://nextgen-wlws.conceptsoftware.eu:9075/SessionTimeout=3600Debug=trueCustProvider=trueCacheSize=1000
1 - WHAT i do:
I am changing a property value (LoginLocale=en_GBbrand) ,changing it (with user input 'teste') and saving it in a File.
2 - HOW i do it:
private Properties props = null;private File resource = null;private String loginLocale = null;public void setLoginLocale(String pLoginLocale) throws IOException { this.loginLocale = String.class.cast(this.props.setProperty("LoginLocale", pLoginLocale)); save();}private void save() { OutputStream os = null; try { os = new FileOutputStream(this.resource); this.props.store(os, null); this.saveFileToRepository(); } catch (IOException e) { e.printStackTrace(); } finally { try { os.close(); } catch (IOException e) { e.printStackTrace(); } }}private void saveFileToRepository() throws FileNotFoundException, IOException { FileReader fr = null; BufferedReader br = null; OutputStream os = null; try { fr = new FileReader(this.resource.toString()); br = new BufferedReader(fr); String fullFile = ""; while (null != (sCurrentLine = br.readLine())) { System.out.println("line" + sCurrentLine); fullFile += sCurrentLine + "\n"; } String directory = getConfigPath(); os = new FileOutputStream(new File(directory)); os.write(fullFile.getBytes()); } finally { if (null != br) { br.close(); } if (null != fr) { fr.close(); } if (null != os) { os.close(); } }}
3 - WHAT results i get:
The code i posted is part of JMX MBean for application (https://blogs.oracle.com/weblogicserver/developing-custom-mbeans-to-manage-j2ee-applications-part-i ), and it automatically updates the line #1 when i change property value, without me doing any explicit code for it.
Regards,
Carlos
P.S. - i think you got cut off in last part of your reply...
-
Hello,
In the meantime i came to a hot fix workaround solution:
fr = new FileReader(this.resource.toString());br = new BufferedReader(fr);String fullFile = "";String finalPart = "";do {//hotfix for issue in reading file with ':' adding backslash int index = fullFile.indexOf("\\:"); String initialPart = fullFile.substring(0, index); finalPart = fullFile.substring(index+1); fullFile = initialPart + finalPart;} while (finalPart.contains("\\:"));String directory = getConfigPath();os = new FileOutputStream(new File(directory));os.write(fullFile.getBytes());
This does not fix the original bug which i still would like to emend.
Regards,
Carlos
-
This is feature of properties files
https://stackoverflow.com/questions/9181572/backslash-issue-in-java-string
-
I am changing a property value (LoginLocale=en_GBbrand) ,changing it (with user input 'teste') and saving it in a File.
Ok - now you see why you need to post ALL of the code and explain WHAT you are doing.
1. you are using JMX to modify a Properties file - not an ordinary file. As others have pointed out Property files support multiple syntax's and thus have escape mechanisms for characters that can be part of the syntax as opposed to part of the 'name' or 'value'.
2. you are then reading that file as an ORDINARY file - not as a Properties file. As an ordinary file there is no special syntax or any code in the reader classes to look for escape characters that are used in property files.
This does not fix the original bug which i still would like to emend.
Any 'bug' would be in the JMX code you are using.
The code i posted is part of JMX MBean for application (https://blogs.oracle.com/weblogicserver/developing-custom-mbeans-to-manage-j2ee-applications-part-i ), and it automatically updates the line #1 when i change property value, without me doing any explicit code for it.
But that code is designed to manipulate Property files and follow the 'syntax' and 'escaping' rules that apply to them.
So it appears that the JMX code is adding the 'escape' characters. For all I know that code could be loading and then rewriting the entire set of keys and values and on the 'write' it escapes characters it thinks should be escaped.
Only a JMX support group/forum can say if that is a 'bug' or is simply 'working as designed'.
So, similar to what I said in #2 above, JMX is treating the file as a property file but you are reading it as an ordinary file.
1. Treat the file as a property file and manipulate it using the Property API
2. Don't use JMX
3. Report the possible bug to the JMX group/forum