×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Regular Contributor
    Join Date
    May 2008
    Location
    Kochi,Kerala,India
    Posts
    105

    Unable to read file.

    I am using this statement for reading file help.txt.
    Code:
    InputStream is = getClass().getResourceAsStream("help.txt");
    but always i am getting exceptions.
    am i doing anything wrong.
    my simple demo's directory structure is this
    demo
    ->src
    ->res
    ->help.txt
    Be hungry,Be foolish
    vivart pandey

  2. #2
    Regular Contributor
    Join Date
    Dec 2007
    Posts
    159

    Re: Unable to read file.

    Please put help.txt file in res folder.

  3. #3
    Regular Contributor
    Join Date
    May 2008
    Location
    Kochi,Kerala,India
    Posts
    105

    Re: Unable to read file.

    I have tried with putting txt file in res folder ,changing url to res/help.txt , /help.txt ,help.txt and
    even i have placed this file in src folder but same exception.
    Be hungry,Be foolish
    vivart pandey

  4. #4
    Nokia Developer Champion
    Join Date
    Jul 2008
    Location
    Pakistan
    Posts
    1,065

    Smile Re: Unable to read file.

    What exception u got? However, the correct code is this (if u r placing txt file in the root of jar) :
    Code:
    InputStream is = getClass().getResourceAsStream("\help.txt");
    After this , Compile the project. Now, open the compiled jar file in any zip manager and check out whether the root of the jar file contains help.txt.If there in no help.txt file in root of jar then u r not adding the resource folder correctly.

    Btw, which IDE u r using? & ur phone?

    Best Regards,
    SajiSoft

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Unable to read file.

    Use a "/", not a "\", at the start of the path.

    Graham.

  6. #6
    Regular Contributor
    Join Date
    May 2008
    Location
    Kochi,Kerala,India
    Posts
    105

    Re: Unable to read file.

    Quote Originally Posted by sajisoft View Post
    What exception u got?
    (is) is null so whenever i am using (is) i am getting NPE.

    Code:
    InputStream is = getClass().getResourceAsStream("\help.txt");
    i think this is not write way because you will get compile time error invalid escape sequence.

    After this , Compile the project. Now, open the compiled jar file in any zip manager and check out whether the root of the jar file contains help.txt.If there in no help.txt file in root of jar then u r not adding the resource folder correctly.
    For this i am going to thank you because in my case compiled jar file is not containing help.txt file and if i am manually placing help.txt file in the jar file, my code is working fine.

    Btw, which IDE(eclipse) u r using? & ur phone(currently i am using wtk emulator if it works, i will go for s60)?

    so now my problem is am i missing any setting of eclipse. why eclipse is not adding txt file in jar automatically?
    Be hungry,Be foolish
    vivart pandey

  7. #7
    Regular Contributor
    Join Date
    May 2008
    Location
    Kochi,Kerala,India
    Posts
    105

    Re: Unable to read file.

    Thanks for all replies my problem solved.
    i have placed help.txt in res dir, my code is
    InputStream is = getClass().getResourceAsStream("help.txt");
    and i have included *.txt in configure build path setting of eclipse.
    Be hungry,Be foolish
    vivart pandey

  8. #8
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Unable to read file.

    The path for getResourceAsStream() is relative to the package of the class. You should prefix the file name with "/" to ensure that you're accessing the file you want. Otherwise, refactoring the code (specifically, changing the package name) might stop it working.

    Cheers,
    Graham.

Similar Threads

  1. Error : Cannot find file : for DUMMIES
    By PACALA_BA in forum Symbian
    Replies: 7
    Last Post: 2008-07-06, 13:18
  2. Unable to read file error
    By gpalvia in forum Digital Rights Management & Content Downloading
    Replies: 0
    Last Post: 2007-11-21, 07:46
  3. Unable to read file DRM forward lock
    By gpalvia in forum Symbian
    Replies: 0
    Last Post: 2007-11-21, 03:58
  4. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×