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.
Is the file itself in the Jar? If so, have you tried reading it as a resource rather than as a file?
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 ...
@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.
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.
Originally Posted by Stormrage
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
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.
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.