开发者

Will this work on Unix?

开发者 https://www.devze.com 2023-01-04 07:26 出处:网络
This Java code lists files in a directory on a Windows shared drive.Will it work correctly on a Unix system?

This Java code lists files in a directory on a Windows shared drive. Will it work correctly on a Unix system?

File directory = ne开发者_如何转开发w File("\\\\server/Shared/stuff/mystuff");
for (File file: directory.listFiles()) {
    System.out.println(file);
}


Short answer: No.

Long answer: Do you have samba installed? Even then you need to mount the the share. So it probably won't work.

EDIT

Java delegates the call to the underlying OS eventually. Since Unix doesn't know what the \\SERVERNAME path means, Java doesn't know what it means either. What you have to do, to get this to work is mount the drive explicitly using Samba. Your other option, if you are running Ubuntu, is look under .gvfs in your home directory. Ubuntu creates a mount there for your Samba shares, which you should be able to access using Java. If you don't want to rely on external tools, try JCIFS for a pure-Java solution.


No... Just let the user select the right path and use an OS dependent file-selection dialog.


On my system (Debian Sid with Gnome 2.30 Desktop) I have to select "smb:///server/Shared/..." to achieve the same behaviour. I think, that GVFS (Gnome Virtual File System) using smbfs drivers handles the real connection in the background...


No, as that is a UNC Path, which is a windowsism.

Are you trying to access a windows share from unix? Then have a look at jcifs.


The counter question I get when seeing this is: "Why would you want to hard-code a path in your application?"

Even if it was just for the example and you intend to load the path from a property file or anything, I still think you are on the wrong track here.

First of all you will want to avoid absolute paths like the plague. Relative paths are sort of ok. You can use slash ('/') characters in paths hardcoded, it will work on both Windows and Linux/Mac. Basically all platforms.

Second of all, why use paths at all? This is the internet age. Use URL's! file: URL's will accomplish the same thing as file paths, but using URL's make your app accept resources from other sources such as web sites and FTP as well.

Third of all, avoid the File class. If you invent a good way to do that, you are out of the woodworks completely. Use URL's together with getResource and getResourceAsStream and your app will work platform independent and across network boundaries over the internet.

0

精彩评论

暂无评论...
验证码 换一张
取 消