• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Passing FileReader an alternative to a file

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, no you are not misunderstanding you are spot on. I am trying to avoid intermediate files. I will try your suggestions now, Thanks.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
InputStreamReader starts with a capital "I".
 
Dan Grindstaff
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Ok I have corrected the i to capital I and even tried wrapping the reader before but still getting error.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's Working! I am getting a parsed file full of wholesome csv goodness thanks to your help!

 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic