Results 1 to 8 of 8
  1. #1
    Stormrage is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default problem with file reader

    Hey guys, I have a question regarding FileReader and FileWriter. Before I ask my question I'd like to give you some details regarding what I'm trying to do. a simple project to keep myself busy.

    I am trying to write a cross-platform RSS feed reader in Java. It basically reads the complete source of a web page, and uses regex and parsing to remove all unnecessary tags in the source, providing the user with clear text.

    I am using a file to save the parsing results at different stages, and this file is read by a FileReader at the end and printed into a JTextArea.

    So far everything was working perfectly, so I created an executable .jar of the complete software to test on windows(I was building on Linux so far). When I tested it on windows it didnt work, so I executed it from command line to see the errors.

    The error is very simple. When reading from the file to the JTextArea, I had written:

    inputStream = new FileReader("/home/JavaProjects/RSS/src/rss_data.txt");

    This brought up the error because I have explicitly entered the linux directory, and as this doesn't apply to windows I need to modify this.

    Note: the "rss_data.txt" file resides in the same directory as my GUI code along with other class files.

    so I tried
    inputStream = new FileReader("/rss_data.txt");

    the error I get is , "file not found".

    which also didn't work. So can anyone tell me how to give a local path, if the main class and text file are in the same directory, how do i tell Java they are in the same local directory.

    Any help will be very appreciated. I am sorry If I didn't explain my problem clearly enough.

    Thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    You might have a look at the "user.dir" or "user.home" System property (read the API for the System.getProperty( ... ) method); maybe you can find your file from there ...

    kind regards,

    Jos

  4. #4
    Stormrage is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    @Fubarable yes the file itself is inside the .jar, and no I have not tried reading it as a resource, in fact I don't think I know how to do that, but i'll do some research on that. thanks a lot man.

    @JosAH ok cool, I don't know much about the "user.dir" or "user.home" system properties. I'll go read the API. thanks a lot dude.

    I'm gonna hit the books for a bit and will let you guys know if I manage to fix this. Thankyou for the very quick responses.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Stormrage View Post
    @JosAH ok cool, I don't know much about the "user.dir" or "user.home" system properties. I'll go read the API. thanks a lot dude.

    I'm gonna hit the books for a bit and will let you guys know if I manage to fix this. Thankyou for the very quick responses.
    If that "file" is stored inside a .jar file it isn't a "file" anymore; it is just an entry in your .jar file and treating it like a file won't work on Linux either. You can get an InputStream for that entry by using the Class.getResourceAsStream( ... ) method. Forget about those System properties; they deal with real files and directories, not .jar entries.

    kind regards,

    Jos

  6. #6
    Stormrage is offline Member
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Default

    hey JosAH I tried the Class.getResourceAsStream() method then later on after reading your post I tried the InputStream.

    First I tried Class.getResourceAsSream() with a FileReader which was stupid, didn't realize I needed an InputStream. anyway then I tried it with InputStream object. It complied fine on my system(linux), but the executable doesn't work on even linux now.

    I think I am getting closer to the solution, because you said I won't be able to run on either platforms. A lot of my classes and their dependent were using FileReaders, I am going for InputStream now. I'll have to change a lot of code, to try this out properly. lol I dont know if I made any sense in this paragraph.

    Anyway, I have a question: the argument for Class.getResourceAsStream(" "), can it be "filename.txt" or shud I omit the ".txt" and just use "filename" as you are saying its just an entry now and not a file in the .jar.

    i really need to learn how to use the InputStream stuff

    thankyou

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Again, this is using resources as I suggested in my first post. I think you should use use the path relative to the class files as your resource name. For instance if you have a file called mydata.txt that is in a directory called "data" just off the class file directory, then use "data/mydata.txt" as the resource name.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    To get an idea of what is in your jar file you can open the jar file in WinZip.
    Or rename the file by adding a .zip to the end of the name and let Window's zip file processor do it.

Similar Threads

  1. Buffered Reader problem
    By pradeep.theonlyone in forum New To Java
    Replies: 3
    Last Post: 07-31-2009, 12:37 PM
  2. Selecting an input file for Reader
    By myazuid in forum New To Java
    Replies: 3
    Last Post: 11-25-2008, 02:21 AM
  3. Java file reader...?
    By prabhurangan in forum New To Java
    Replies: 3
    Last Post: 11-21-2008, 09:19 AM
  4. [SOLVED] Need help with file reader
    By syed.shuvo in forum New To Java
    Replies: 6
    Last Post: 09-27-2008, 08:43 PM
  5. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:03 AM

Posting Permissions

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