This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes What FileReader expects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "What FileReader expects" Watch "What FileReader expects" New topic
Author

What FileReader expects

Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hello All, I have the following program in which FileReader expects an argument. I am wondering why I can't get away with the full ftp path to the file.



Any help is greatly appreciated!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

FR expects a file name, that is, a path to a file on the local file system. What you gave it is a URL that requires an FTP client to resolve. FR is not an FTP client, and it's not meant for handling arbitrary URLs. You can't give it an HTTP URL either, even though that may eventually resolve to the static contents of a single file.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Hi, Thank you for the response. It sounds like I will need to import the file first? But I am wondering if there is a way around this?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dan Grindstaff wrote:Hi, Thank you for the response. It sounds like I will need to import the file first? But I am wondering if there is a way around this?


If by "import" you mean copy it to your local file system by some other means, then, yes, that's one option.

Note also that there do exist FTP clients for Java, which you can find by googling. It's just that the FileReader class isn't one of them.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Ok, I added the following code to try and fetch the file though I would rather just erad it from where it sits.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dan Grindstaff wrote:Ok, I added the following code to try and fetch the file though I would rather just erad it from where it sits.


Does that FTP client offer a "spit back the contents in a stream but don't save them to a file" operation? If so, then that's what you'd use. If not, then you could see if there's some other FTP client that does so, or some other service on that host making that file available where you could do that, with a corresponding client.

For example: Apache commons FTPClient.retrieveFileStream(java.lang.String)
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
I have switched gears, trying to find an FTP solution and THEN I can concentrate on the CSV parser. I have tried this code for FTP:

which returns the following error:
I am completely stuck right now.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

It looks like you're trying to use "ftp://<some address>/production" as the host name or address, when in fact it should just be "<some address>".
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Ok, no errors when I just have the address in there, however the file I need is still a couple directories further into the url. Here is the code:
and here is the error message:
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Ok, this code does not throw errors:
But I have a stupid question. Where am I downloading the file to?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Note that the root that the FTP server shows you will not necessarily be that hosts file system root. Typically it is not. You have to either know ahead of time what the path will be relative to wherever the FTP sets your user's root at, or you have to look for it. The FTP protocol provides a directory listing operation, and I would assume your client library provides access to it.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
All Right! Making progress I found the file in the code directory but it is empty! Any ideas why it would be empty?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dan Grindstaff wrote:But I have a stupid question. Where am I downloading the file to?


Well, you have this:


So it's going to the path specified by whatever you set filename to. If you didn't make it an absolute path, it will be relative to whatever the current directory was for the JVM at the time it was started. It's better not to rely on that, but to specify an absolute path instead. Or, if you have to rely on it, you can ask the File object referred to by filename what its absolute path is.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Dan Grindstaff wrote:All Right! Making progress I found the file in the code directory but it is empty! Any ideas why it would be empty?


No, I don't. I thought maybe you didn't close the FOS, but it looks like you did.

I'd start digging into the docs for the client library, see what kind of diagnostics it can give you, add some println() statements so you can see what's going on, maybe read a stream first instead of just retreiveFile, and see what's in that stream.

In other words, as with any debugging, you have to break it down into smaller and smaller pieces and to find out exactly where the problem is occurring.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Could you please give me an example of how to use RetrieveFile? Thanks Never mind I see I am already using it in one place. Duh!
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Ok, I have some code here that is working:

but I need a way to pass the line read from this code to the reader in :

Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
It seems like I am getting really close with the following code:



But I am mangling up something in the write section.
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Ok, I finally got some code to accomplish this:



I would appreciate any help in cleaning it up and/or hints on how to further simplify. Thanks!
Dan Grindstaff
Ranch Hand

Joined: Sep 24, 2006
Posts: 138
Dan Grindstaff wrote:Ok, I finally got some code to accomplish this:



I would appreciate any help in cleaning it up and/or hints on how to further simplify. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What FileReader expects