Creating a File with relative path

edited December 31, 1969 in Designing Analytics Reports
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

  • mwilliamsmwilliams BIRT Guru
    edited December 31, 1969
    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.

    Regards,

    Michael Williams

    eSignLive Evangelism & Community Manager | eSignLive by VASCO
     
    Find me on:
    Twitter
    Facebook
    Blog
    LinkedIn

    eSignLive Developer Community
     
    Email me:
    Google: [email protected]

  • edited December 31, 1969
    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.
  • mwilliamsmwilliams BIRT Guru
    edited December 31, 1969
    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.

    Regards,

    Michael Williams

    eSignLive Evangelism & Community Manager | eSignLive by VASCO
     
    Find me on:
    Twitter
    Facebook
    Blog
    LinkedIn

    eSignLive Developer Community
     
    Email me:
    Google: [email protected]

  • edited December 31, 1969
    Hi Williams,



    I tried this..



    resourceFolderPath = reportContext.getDesignHandle().resourceFolder;

    file = new File(resourceFolderPath+"\\"+"hwatermark.properties");



    and the absolute path of the file I got is..



    C:\devl\ws-svn\MR\null\hwatermark.properties.



    I don't understand the null there.. Could you help me out with where I'm going wrong..



    Thanks,

    EVSGK.
  • mwilliamsmwilliams BIRT Guru
    edited December 31, 1969
    Try adding just a single '\'. The null probably comes between the two that you add.

    Regards,

    Michael Williams

    eSignLive Evangelism & Community Manager | eSignLive by VASCO
     
    Find me on:
    Twitter
    Facebook
    Blog
    LinkedIn

    eSignLive Developer Community
     
    Email me:
    Google: [email protected]

  • edited December 31, 1969


    Try adding just a single '\'. The null probably comes between the two that you add.






    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..
  • mwilliamsmwilliams BIRT Guru
    edited December 31, 1969
    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.

    Regards,

    Michael Williams

    eSignLive Evangelism & Community Manager | eSignLive by VASCO
     
    Find me on:
    Twitter
    Facebook
    Blog
    LinkedIn

    eSignLive Developer Community
     
    Email me:
    Google: [email protected]

  • edited December 31, 1969


    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.






    Didn't worked..Back to same problem..
  • mwilliamsmwilliams BIRT Guru
    edited December 31, 1969
    Have you tried the reportContext.getResource("yourfile"); way?

    Regards,

    Michael Williams

    eSignLive Evangelism & Community Manager | eSignLive by VASCO
     
    Find me on:
    Twitter
    Facebook
    Blog
    LinkedIn

    eSignLive Developer Community
     
    Email me:
    Google: [email protected]

  • edited December 31, 1969
    I tired this...



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

    file = new File(resourceFilePath);



    And I got the below text when I tried to set the text of a label as

    this.text = file.getAbsolutePath();



    The text is: C:\devl\ws-svn\MR\file:\C:\devl\ws-svn\MR\hwatermark.properties



    I don't understand why I got C:\devl\ws-svn\MR\ repeated twice in the absolute path..



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



    Regards,

    EVSGK.
  • mwilliamsmwilliams BIRT Guru
    edited November 3, 2011
    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.

    Regards,

    Michael Williams

    eSignLive Evangelism & Community Manager | eSignLive by VASCO
     
    Find me on:
    Twitter
    Facebook
    Blog
    LinkedIn

    eSignLive Developer Community
     
    Email me:
    Google: [email protected]

  • mwilliamsmwilliams BIRT Guru
    edited December 31, 1969
    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");

    Regards,

    Michael Williams

    eSignLive Evangelism & Community Manager | eSignLive by VASCO
     
    Find me on:
    Twitter
    Facebook
    Blog
    LinkedIn

    eSignLive Developer Community
     
    Email me:
    Google: [email protected]

  • edited December 31, 1969
    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..
  • mwilliamsmwilliams BIRT Guru
    edited December 31, 1969
    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!?

    Regards,

    Michael Williams

    eSignLive Evangelism & Community Manager | eSignLive by VASCO
     
    Find me on:
    Twitter
    Facebook
    Blog
    LinkedIn

    eSignLive Developer Community
     
    Email me:
    Google: [email protected]

  • @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??

Sign In or Register to comment.