determine file type error
Hello to everyone.
I am developing my year 3 project and i have run into some trouble.
I am trying to determine whether a file is or is not a directory. Using the file.isDirectory works flawlessly, locally on my computer. I am trying to do this remotely using sockets as well. I send a File[] with all the directory contents of a specific folder.
Doing so, causes a very few folders to be identified as directories where as others return false when checked using the isDirectory method. I have checked to see if it is a permissions problem outside Java but no such case is valid, so i am assuming that it should be within my code. As i already mentioned, working locally works just fine.
Anyone has any idea what might be causing such a problem? Any help would really be appreciated.
Kind regards
Chris
Are you getting a SecurityException?
Well the way i have it returns nothing. It wont identify the files as plain files nor as directories. It just prints false (is not directory) and hangs, Here's the code:
int selectedIndex = list.getSelectionIndex();
System.out.println(client.rec[selectedIndex].isDirectory()); //Prints true or false if it is a directory
if (client.rec[selectedIndex].isDirectory()) {
text.setText(client.rec[selectedIndex].getPath());
try {
client.sendData("getFS", text.getText());
client.receiveData();
if (client.com.equals("fileSystem")) {
list.removeAll();
addToList(client.rec);
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
text.setText(client.rec[selectedIndex].getPath());
}
Note that client.rec is the File[] and the SelectedIndex is the index in a list. I have tried substituting the isDirectory()
with isFile() == false
which enters the if statement but when selecting a file it returns a NullPointerException
> Hello to everyone.
> I am developing my year 3 project and i have run into
> some trouble.
> I am trying to determine whether a file is or is not
> a directory. Using the file.isDirectory works
> flawlessly, locally on my computer. I am trying to do
> this remotely using sockets as well. I send a File[]
> with all the directory contents of a specific
> folder.
Huh? You 'send' what?
Are you sending File array via a socket to another computer.
That isn't a good idea. If you need to sync file/dirs between computers then send the dirs and files and text strings.
And I have no idea why File even supports Serializable. But presumably if you are sending that the version on the other computer is going to end up telling you that the files don't exist. Neither sending an array (File) nor even sending File by itself is going to transfer directories or files.
If you want to transfer files then look into FTP (File Transfer Protocol.)
File is serializable? What do you know!
The first thing that popped into my mind is that some properties, like isDirectory,
may loaded lazily. So it you serialize a File object across a network, then
ask it if it is a directory, it may check against the current (local) file system
instead of the file system it originated against.
My advice is to not send File objects. I don't know what you are doing,
what your goal is, but perhaps encapsulating the info you need in
an object of a serializable class you define (FileInfo?) will fix this problem.
Thanks for the fast reply to everyone.
FTP is not a choice as this is not my target. I am trying to implement a client/server model between a computer and a PDA(or another computer perhaps). I am unaware of the similarities of a file and a file array but i assumed that by sending a file array would just send a list of the files in a current directory along with their properties (i.e. if it is a file or directory) and i thought of sending this list just to save bandwidth (not too much exchange of data between client/server) Thanks to your answers now i know why using another computer would only open WINDOWS, Program files and Documents And settings folders.
So how is it possible to grab a whole list of files including their properties?
By the way File is not serializable. I am sending files by transforming them into byte[]. I just did not know the similarities between files and file[]... and now i feel plain dumb
Thanks
Hmmm... File isn't serializable?Then someone should open a bug on those java docs.... http://java.sun.com/javase/6/docs/api/java/io/File.htmlOr perhaps Serializable just doesn't mean what it used to mean?
My bad let me rephrase:
I am not sure if file is or is not serializable but what i am doing at the moment is converting a file to a byte[] and send it.
I am not trying to be ironic btw but i dont like talking about things that i am unaware of as other forum members might get a wrong impression.
Thanks jschell
> So how is it possible to grab a whole list of files including their properties?
You want the data in the files correct?
So the first step is to implement your own version of FTP. Note that jakarta commons has FTP (client and server) already. But if you must then you are going to have to read it on one end as bytes and then send it.
Then you need to collect the properties. What ever you consider important. Presumably you might want to collect the location as well. You might keep in mind that that is not as easy as it sounds. Consider what happens if you move files from a computer that has a D: drive to one that only has a C: drive.
All of the above is one 'File'.
And you have multiples of them.
So you need to collect the above into a grouping and send the entire group.
Finally you should not send data on a socket. You should send messages that contain data.)
I am not quite clear on whether you are attempting to send an array of actual Files or an array of file names - that later being what you get from:
File file = new File("myFile");
if ( file.isDirectory )
String[] files = file.list();
I was under the impression that you were sending an array of file names and then attempting to see if they exist on a remote computer and if so to define what they are. If so, I can't see why you would do that.
If you are attempting to send actual File Objects - which is what I did not think you were attempting to do, than that is very bad security wize. This is why SFTP has made obsolete FTP, and I would think that this is even worse.
UPDATE: read the two latest posts after posting mine - nevermind.
I can transfer the files. Already done it and they are placed within the directory my jar file is placed. Along with the byte[] i also send a String with the file's filename so i just open a fileoutput stream, create a file in the current working directory named whatever the String fileName returns and write the bytes in the file. What i am not able to do at the moment is, before requesting a file, getting a list of files in a predefined remote directory along with their properties (when i talk about files i mean both files and directories).
hmmmm maybe i should state clearly what i am doing (or atleast try doing):
-Server opens a port and awaits for connection
-Client connects to the server and sends a request with all files in the servers root directory
-Server responds and sends a list of files and directories
-Client selects a directory and requests a list with all the files and directories within that folder
- OR -
-Client requests a file of his interest to be downloaded on his machine.
-Files are encoded into byte[] and sent
I am able to get a list with the servers root folder and when selecting a file i am able to download it. My problem is that when selecting a directory i cannot distinguish if it is a directory or not. I was trying doing this locally by sending a file[] from the server to the client and determining client-side if the file is or is not a directory but thanks to everyone's answers, that is not possible, so i will have to send a request each time to the server to verify if the file's type.
My question is... is there any easier way of doing so? When requesting a list of files is it also possible sending their properties so i wont have to flood the server with requests?
This might sound like FTP but i do not want to implement an FTP server using the Java's libraries so i am following this path. Security is not of my concern at the moment.
Thanks to everyone
I still have no idea what you are doing. When you say "I can transfer the files" I get the impression you are actually transferring the contents of the files. But at any rate, if you have a File object in your program then it refers to a file in the local machine. It may or may not have been a directory in some other machine but there is no way of finding that out. All you can find is whether it is a directory on this machine.
> -Server responds and sends a list of files and directoriesSorry, still not clear. The server would be sending a list of names of files and directories?
Hmmm well the server sends a file array. This is the code:
File file = new File(path);
File fileSystem[] = file.listFiles();
I am not sure what this returns but i was hoping to return a list of filenames along with their properties. I tried sending a String[] using the file.list() but as it was not convenient at the time i dropped that solution and went to the easy way (according to my judgment).
Btw excuse me for my poor English but i don't come from an English speaking country :o)
listFiles
public File[] listFiles()
Returns an array of abstract pathnames denoting the files in the directory
denoted by this abstract pathname.
If this abstract pathname does not denote a directory, then this method
returns null. Otherwise an array of File objects is returned, one for each file
or directory in the directory. Pathnames denoting the directory itself and the
directory's parent directory are not included in the result. Each resulting
abstract pathname is constructed from this abstract pathname using the
File(File, String) constructor. Therefore if this pathname is absolute then
each resulting pathname is absolute; if this pathname is relative then each
resulting pathname will be relative to the same directory.
There is no guarantee that the name strings in the resulting array will appear
in any specific order; they are not, in particular, guaranteed to appear in
alphabetical order.
Returns:
An array of abstract pathnames denoting the files and directories in the
directory denoted by this abstract pathname. The array will be empty if the
directory is empty. Returns null if this abstract pathname does not denote a
directory, or if an I/O error occurs.
Throws:
SecurityException - If a security manager exists and its
SecurityManager.checkRead(java.lang.String) method denies read access to
the directory
