Results 1 to 8 of 8
Thread: problem with file reader
- 05-15-2010, 02:51 PM #1
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
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.
-
Is the file itself in the Jar? If so, have you tried reading it as a resource rather than as a file?
- 05-15-2010, 02:58 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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
- 05-15-2010, 03:07 PM #4
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
@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.
- 05-15-2010, 03:14 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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
- 05-15-2010, 10:54 PM #6
Member
- Join Date
- May 2010
- Posts
- 3
- Rep Power
- 0
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
-
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.
- 05-15-2010, 11:48 PM #8
Similar Threads
-
Buffered Reader problem
By pradeep.theonlyone in forum New To JavaReplies: 3Last Post: 07-31-2009, 11:37 AM -
Selecting an input file for Reader
By myazuid in forum New To JavaReplies: 3Last Post: 11-25-2008, 01:21 AM -
Java file reader...?
By prabhurangan in forum New To JavaReplies: 3Last Post: 11-21-2008, 08:19 AM -
[SOLVED] Need help with file reader
By syed.shuvo in forum New To JavaReplies: 6Last Post: 09-27-2008, 07:43 PM -
help with file reader
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks