This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Passing FileReader an alternative to a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Passing FileReader an alternative to a file" Watch "Passing FileReader an alternative to a file" New topic
Author

Passing FileReader an alternative to a file

Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hello All, I have some code that will read a file from a URL and write it to disk. I then have some CSV parser code that can read and parse that file. I am trying to figure out a way to pass the file as a data object to the FileReader of the CSV pars3er code to avoid writing anything to disk. Any help is greatly appreciated! Here is the CSV code:

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Read the contents from the URL into a byte[] or a String. Then use a ByteArrayInputStream or StringReader to read from that.

Or, since you appear to be processing a line at a time, just skip the part where you store the whole thing locally, and instead read a line, process it, and send it to whatever its ultimate destination is.

Although it's not really clear to me what you're trying to accomplish, or what the actual problem is. It looks like you're trying to read a file and process it without creating any intermediate temp files, but I may be misunderstanding.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hi, no you are not misunderstanding you are spot on. I am trying to avoid intermediate files. I will try your suggestions now, Thanks.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
If the CSVReader class has a constructor which accepts an InputStream or Reader you may be able to get the stream that reads the file from the URL and parse it directly to the CSVReader class' constructor.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hi Tony, thanks for writing. Well all I have been able to come up with is combining the classes. I spoke with my super and yes indeed he wants a csv-free file at the end of the process on disk. But I know there has to be a way to pass the stream into the Reader directly but I am not seeing it.

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2173
    
  47
Take the InputStream you get from calling url.openStream(), wrap it in an java.io.InputStreamReader and pass that to the CSVReader constructor.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hi Tony, Is this more like what you are talking about?

I'm getting an error at the wrapper java.io cannot be resolved to a type.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

InputStreamReader starts with a capital "I".
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hi, Ok I have corrected the i to capital I and even tried wrapping the reader before but still getting error.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dan Grindstaff wrote:Hi, Ok I have corrected the i to capital I and even tried wrapping the reader before but still getting error.


What's the exact, complete error message, and what line is causing it?
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hi, here is the code with a Reader wrapping the input stream:


The error message at line 6, FileReader(newReader) gives the error The constructor FileReader(Reader) is undefined. The suggested fix is change type of newReader to String.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Now it looks like you're just guessing. Just throwing code up against the wall to see what sticks.

Why did you think you could pass an arbitrary Reader to a FileReader constructor? What did you see in FR's docs that told you that?

Why are you even trying to use FileReader, given that you are trying to get away from local files altogether?

You have some CSV data "out there somewhere," right?

You're able to get to that CSV data with an FTP client library, right?

You want to process that data a line at a time, and you don't want to create any local temp files to do so, right?

So, assuming the CSVReader class lets you process a line at a time the way you want, then what you need is a way to connect CSVReader's input to the output of the FTPClient that's getting the data from "out there," yes?

I don't know what kinds of inputs CSVReader can take, and I don't know what kinds of outputs the FTP client can give, but if the FTP client gives you an InputStream or a Reader, and the CSVReader can accetp an InputStream or a Reader, it should be either a direct connection or at most one layer of wrapping.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Thanks Jeff. OK I have looked at the javadoc for CSVReader again and it says it accepts Reader to construct so I dropped the FileReader and directly inserted my newReader so the code looks like this:

I'm getting this error:

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Okay, I'm not sure why you're still using a FileWriter to create a temp file. I thought you were supposed to avoid that completely.

As for the error: Read the error message carefully. It says, "IOException: Stream closed", and we can see this is happenign when you call readNext() at line 66 of ReadFile.java. That tells us everything we need to know. Why do you think it might be complaining about the stream being closed when you call readNext()? What do you supposed might cause that? And as an extra hint, consider that it must be a bug in your code. So what did you do in your code to cause a read call to fail due to something being closed?
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Ok I have removed the *.close() syntax just see if I can get a file written and now it is writing but the file is empty.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dan Grindstaff wrote:Ok I have removed the *.close() syntax just see if I can get a file written and now it is writing but the file is empty.


Calling close() isn't wrong. It's always a good idea, and sometimes it's necessary. Your problem was you were trying to keep using the stream or reader or writer after closing it.

The empty file may be caused by not calling close(), or it may be something else. Maybe your'e not writing any data to the file. One way to test that would be to call System.out.println() to print every line to the console immediately before writing it to the file.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
It's Working! I am getting a parsed file full of wholesome csv goodness thanks to your help!

 
Don't get me started about those stupid light bulbs.
 
subject: Passing FileReader an alternative to a file
 
Similar Threads
What FileReader expects
Printing a line of array fields
extract data from .csv
how to write to a csv file ??
Java File rename from Excel columns CODE doesn't work with files with 10 characters in their name