1

I have connected to an ftp location using;

URL url = new URL("ftp://user:[email protected]/" + file_name +";type=i");

I read the content into a byte array as shown below;

byte[] buffer = new byte[1024];
int count = 0;
while((count = fis.read(buffer)) > 0) 
{
    //check if bytes in buffer is a file
}

I want to be able to check if the bytes in buffer is a file without explicitly passing a specific file to write to it like;

    File xfile= new File("dir1/");
    FileOutputStream fos = new FileOutputStream(xfile);
    fos.write(bytes);

    if(xfile.isFile())
    {

    }

In an Ideal world something like this;

    File xfile = new File(buffer);//Note: you cannot do this in java
    if(xfile.isFile())
    {

    }

isFile() is to check if the bytes read from the ftp is file. I don't want to pass an explicit file name as I do not know the name of the file on the ftp location.

Any solutions available?

2
  • 2
    What do you mean by isFile()? You mean a valid content for e.g. a Excel Sheet? Java cannot know that. Commented Aug 24, 2011 at 11:14
  • 3
    Your question doesn't make much sense. File.isFile() checks if the file's pathname denotes a regular file (instead of e.g. a directory) in the server's filesystem. The content of your byte array could be anything, but whatever it is, once you write it to your harddisk, it becomes a file (i.e. the content of a file). There is no possible content that cannot become a file once you write it. Commented Aug 24, 2011 at 11:15

4 Answers 4

2

What is a file?

A computer file is a block of arbitrary information [...] which is available to a computer program and is usually based on some kind of durable storage. A file is durable in the sense that it remains available for programs to use after the current program has finished.

Your bytes that are stored in the byte array will be a part of a file if you write them on some kind of durable storage.

Sure, we often say that we read a file or write a file, but basically we read bytes from a file and write bytes to a file.

So we can't test a byte array whether it's content is a file or not. Simply because every byte array can be used to create a file (even an empty array).

BTW - the ftp server does not send a file, it (1) reads bytes and (2) a filename and (3) sends the bytes and (4) the filename so that a client can (5) read the bytes and (6) the filename and use both datasets to (7) create a file. The ftp server doesn't have to access a file, it can take bytes and names from a database or create both in memory...

Sign up to request clarification or add additional context in comments.

2 Comments

How do I retrieve the file name? Thanks.
Actually depends on the service. With FTP the client knows and sends the filename. It may request a list of filenames first (some dir command)
1

I guess you cannot check if the byte[] array is a file or not. Why dont' you just use already written and tested library like maybe for example: http://commons.apache.org/net/

Comments

1

There is no way to do that easily.

A file is a byte array on a disk and a byte array will be a file if you write it to disk. There is no reliable way of telling what is in the data you just received, without parsing the data and checking if you can find a valid file header in it.

Comments

0

Where is isFile() file means the content fetched from from the ftp stream is a file.

The answer to that is simple. You can't do it because it doesn't make any sense.

  • What you have read from the stream IS a sequence of bytes stored in memory.

  • A file is a sequence of bytes stored on a disk (typically).

These are not the same thing. (Or if you want to get all theoretical / philosophical you have to answer the question "when is a sequence of bytes a file, and when is it not a file".


Now a more sensible question to ask might be:

How do I know if the stuff I fetched by FTP is the contents of a file on the FTP server.

(... as distinct from a rendering of a directory or something).

The answer is that you can't be sure if you fetched the file by opening an URLConnection to the FTP server ... like you have done. It is like asking "is '(123) 555-5555' a phone number?". It could be a phone number, or it could just be a sequence of characters that look like a phone number.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.