Creating a File with relative path

vensw
edited February 11, 2022 in Analytics #1
Hi All,

I'm trying to read a properties file say(testtemplate.properties) which is on Desktop.
I don't want to use absolute path. How can I specify that?

Example Code: I'm writing the below code in the Report's beforeFactory() method.

importClass(java.io.File);
importClass(java.io.BufferedReader);
importClass(java.io.InputStreamReader);
importClass(java.io.FileInputStream);

file = new File("C:/Documents and Settings/student/Desktop/testtemplate.properties");

try {
if (file.exists()) {
// If Logic goes here
} else {
// Else Logic goes here
}
} catch(exception) {

}


The above code works fine since the path I gave is an absolute path. But I need to specify relative path.
I tried

file = new File("testtemplate.properties");

and it didn't worked.

I also added the testtemplate.properties file in the Shared Resources. Also, I noticed the below code in the XML Source of my report.

<simple-property-list name="includeResource">
<value>testtemplate</value>
</simple-property-list>


Kindly, help me in finding out where I'm going wrong. Thank you for your time and patience..

Regards,
EVSGK.

Comments

  • mwilliams
    edited December 31, 1969 #2
    You can grab the resource folder path with:

    reportContext.getDesignHandle().resourceFolder;

    So, you could possibly use this path to help you with creating a relative path.
    Warning No formatter is installed for the format ipb
  • vensw
    edited December 31, 1969 #3
    Hi Williams,

    Could you please provide me an example showing how to access a file from the resource folder? Thank you so much for the help.

    Regards,
    EVSGK.
  • mwilliams
    edited December 31, 1969 #4
    If you put your file in the resource folder, you should be able to use:

    reportContext.getResource("yourfile");

    This will return the path to the file which you then can open and read. Let me know.
    Warning No formatter is installed for the format ipb
  • vensw
    edited December 31, 1969 #5
    Hi Williams,<br />
    <br />
    I tried this..<br />
    <br />
    resourceFolderPath = reportContext.getDesignHandle().resourceFolder;<br />
    file = new File(resourceFolderPath+"\\"+"hwatermark.properties");<br />
    <br />
    and the absolute path of the file I got is..<br />
    <br />
    C:\devl\ws-svn\MR\<span style='color: #FF0000'>null</span>\hwatermark.properties.<br />
    <br />
    I don't understand the null there.. Could you help me out with where I'm going wrong..<br />
    <br />
    Thanks,<br />
    EVSGK.
  • mwilliams
    edited December 31, 1969 #6
    Try adding just a single '\'. The null probably comes between the two that you add.
    Warning No formatter is installed for the format ipb
  • vensw
    edited December 31, 1969 #7
    <blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="84848" data-time="1320333579" data-date="03 November 2011 - 08:19 AM"><p>
    Try adding just a single '\'. The null probably comes between the two that you add.<br /></p></blockquote>
    <br />
    <br />
    If you add only one '\' like "\", it assumes we are trying to enter escape sequence \" ( which inserts a double quote character in the text at that point). In order to include \ in the string, we have to prepend it with another \. Let me know if I'm wrong..
  • mwilliams
    edited December 31, 1969 #8
    Yeah, but it just seems that that's where the issue is. Did you try it? If you did and it didn't work. Try leaving out the slash altogether.
    Warning No formatter is installed for the format ipb
  • vensw
    edited December 31, 1969 #9
    <blockquote class='ipsBlockquote' data-author="'mwilliams'" data-cid="84855" data-time="1320341860" data-date="03 November 2011 - 10:37 AM"><p>
    Yeah, but it just seems that that's where the issue is. Did you try it? If you did and it didn't work. Try leaving out the slash altogether.<br /></p></blockquote>
    <br />
    <br />
    Didn't worked..Back to same problem..
  • mwilliams
    edited December 31, 1969 #10
    Have you tried the reportContext.getResource("yourfile"); way?
    Warning No formatter is installed for the format ipb
  • vensw
    edited December 31, 1969 #11
    I tired this...<br />
    <br />
    resourceFilePath = reportContext.getResource("hwatermark.properties");<br />
    file = new File(resourceFilePath);<br />
    <br />
    And I got the below text when I tried to set the text of a label as<br />
    this.text = file.getAbsolutePath(); <br />
    <br />
    The text is: <strong class='bbc'>C:\devl\ws-svn\MR\file:\C:\devl\ws-svn\MR\hwatermark.properties</strong><br />
    <br />
    I don't understand why I got C:\devl\ws-svn\MR\ repeated twice in the absolute path..<br />
    <br />
    Could you please provide me an example of how to read a properties file which is in the resources folder of the report? Thanks a bunch...<br />
    <br />
    Regards,<br />
    EVSGK.
  • mwilliams
    edited November 3, 2011 #12
    If I do:

    file = new File("testtemplate.properties");

    then set the value of a text box to be the absolute path of this file, I get:

    C:\Eclipse BIRT\BIRT 2_6_2\testtemplate.properties

    Which is where Eclipse is installed, so the default location is BIRT HOME. So, if I were to build a path to the same location and then call new File on that location, I would probably get a double path the same as you have. So, if you just make sure your resource folder is where you want the file, you should be able to just do:

    file = new File("testtemplate.properties");

    And the path will probably be to your BIRT install folder, just like mine does above. What is your BIRT version? Maybe that's the difference.
    Warning No formatter is installed for the format ipb
  • mwilliams
    edited December 31, 1969 #13
    This worked for me to point directly to the resource folder. A single forward slash '/' does it.

    setFileLocation = reportContext.getDesignHandle().resourceFolder;
    file = new File(setFileLocation + "/" + "testtemplate.properties");
    Warning No formatter is installed for the format ipb
  • vensw
    edited December 31, 1969 #14
    I tried the below approaches..<br />
    <br />
    Approach 1:<br />
    <br />
    resourceFilePath = reportContext.getDesignHandle().resourceFolder;<br />
    file = new File(resourceFilePath + "/" + "watermark.properties");<br />
    <br />
    Observed:<br />
    1) View Report As PDF : Worked Fine. <br />
    2) When ran Integration Test : null is being appended in the path and <br />
    the path Is:C:\devl\ws-svn\MR\<span style='color: #FF0000'>null</span>\watermark.properties <br />
    3) When ran the job on Server : null is being appended in the path and <br />
    the path Is:C:\Program Files\IBM\SDP\runtimes\base_v7\profiles\was70profile1\bin\<span style='color: #FF0000'>null</span>\watermark.properties<br />
    <br />
    <br />
    Approach 2:<br />
    <br />
    resourceFilePath = reportContext.getResource("watermark.properties");<br />
    file = new File(resourceFilePath.toString().substring(6));<br />
    <br />
    // reportContext.getResource("watermark.properties") is returning <br />
    <span style='color: #006400'>file:/C:\devl\ws-svn\MR\<span style='color: #FF0000'>null</span>\watermark.properties</span><br />
    <br />
    Observed:<br />
    1) View Report As PDF : Worked Fine. <br />
    2) When ran Integration Test : Worked Fine. <br />
    3) When ran the job on Server : toString() is being called on null and an error is thrown.<br />
    <br />
    <br />
    Approach 3:<br />
    <br />
    <br />
    importPackage(com.mr.marketing.birt.watermark)<br />
    <br />
    <span style='color: #006400'>waterMarkReader = new WaterMarkPropertyReader();</span><br />
    // WaterMarkPropertyReader is a class in the package com.mr.marketing.birt.watermark. <br />
    <span style='color: #006400'>propertyValue = waterMarkReader.getWaterMarkKeyValue("hasWaterMark");</span><br />
    getWaterMarkKeyValue("hasWaterMark") will return the property value of the key that was passed to it. (In this case, it is hasWaterMark).<br />
    <br />
    <br />
    Observed:<br />
    1) View Report As PDF : path is:C:\Program Files\ibm\SDP\watermark.properties <br />
    2) When ran Integration Test : path is:C:\devl\ws-svn\MR\watermark.properties <br />
    3) When ran the job on Server : path is:C:\Program Files\IBM\SDP\runtimes\base_v7\profiles\was70profile1\bin\watermark.properties<br />
    <br />
    Birt is expecting the watermark.properties file from three different places this time..
  • mwilliams
    edited December 31, 1969 #15
    I haven't seen the null in mine. Any way you can attach a report that I can run that will show this. Maybe if we're working on the same file, I can be of more help!?
    Warning No formatter is installed for the format ipb
  • @vensw said:
    I tried the below approaches..

    Approach 1:

    resourceFilePath = reportContext.getDesignHandle().resourceFolder;

    file = new File(resourceFilePath + "/" + "watermark.properties");

    Observed:

    1) View Report As PDF : Worked Fine.

    2) When ran Integration Test : null is being appended in the path and

    the path Is:C:\devl\ws-svn\MR\null\watermark.properties

    3) When ran the job on Server : null is being appended in the path and

    the path Is:C:\Program Files\IBM\SDP\runtimes\base_v7\profiles\was70profile1\bin\null\watermark.properties

    Approach 2:

    resourceFilePath = reportContext.getResource("watermark.properties");

    file = new File(resourceFilePath.toString().substring(6));

    // reportContext.getResource("watermark.properties") is returning

    file:/C:\devl\ws-svn\MR\null\watermark.properties

    Observed:

    1) View Report As PDF : Worked Fine.

    2) When ran Integration Test : Worked Fine.

    3) When ran the job on Server : toString() is being called on null and an error is thrown.

    Approach 3:

    importPackage(com.mr.marketing.birt.watermark)

    waterMarkReader = new WaterMarkPropertyReader();

    // WaterMarkPropertyReader is a class in the package com.mr.marketing.birt.watermark.

    propertyValue = waterMarkReader.getWaterMarkKeyValue("hasWaterMark");

    getWaterMarkKeyValue("hasWaterMark") will return the property value of the key that was passed to it. (In this case, it is hasWaterMark).

    Observed:

    1) View Report As PDF : path is:C:\Program Files\ibm\SDP\watermark.properties

    2) When ran Integration Test : path is:C:\devl\ws-svn\MR\watermark.properties

    3) When ran the job on Server : path is:C:\Program Files\IBM\SDP\runtimes\base_v7\profiles\was70profile1\bin\watermark.properties

    Birt is expecting the watermark.properties file from three different places this time..

    Did you find any solution for the relative path??