Forum Stats

  • 3,826,400 Users
  • 2,260,641 Discussions
  • 7,896,931 Comments

Discussions

backslash added while reading File

managed BEAN
managed BEAN Member Posts: 948 Silver Badge
edited Feb 27, 2018 9:37AM in Java Programming

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:

pastedImage_1.png

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

managed BEAN

Answers

  • Unknown
    edited Feb 21, 2018 12:40PM
    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:
    1. #TueFeb2008:40:24WET2018
    2. DataEngineFactory=dummy.app.EngineFactory
    3. PersistCache=false
    4. LoginLocale=en_GBbrand
    5. ServiceRoot=https://nextgen-wlws.conceptsoftware.eu:9075/
    6. SessionTimeout=3600
    7. Debug=true
    8. CustProvider=true
    9. 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:
    pastedImage_1.png

    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.

  • managed BEAN
    managed BEAN Member Posts: 948 Silver Badge
    edited Feb 21, 2018 4:17AM

    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:

    pastedImage_1.png

    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...

  • managed BEAN
    managed BEAN Member Posts: 948 Silver Badge
    edited Feb 21, 2018 4:25AM

    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

  • rpc1
    rpc1 Member Posts: 1,503
    edited Feb 21, 2018 5:06AM
  • Unknown
    edited Feb 21, 2018 12:58PM
    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

    managed BEANmanaged BEAN
This discussion has been closed.