Re: File Scanning on a Unix Box
- From: Nigel Wade <nmw@xxxxxxxxxxxx>
- Date: Mon, 04 Apr 2005 10:53:38 +0100
Rishi Dhupar wrote:
> Hi,
> I have written this recursion File scan, that is working perfectly in
> Windows but of course just won't work in Unix.
>
> Pretty simple, just goes thru directories and subdirectories and adds
> any File it finds to a vector of type FileInfo. Any idea on how to get
> to this to work on a Unix box?
>
> Also should I be running the scan on the mounted name or the file
> system name? Both do not seem to be working. Not too familiar with how
> Unix filesystems work. For instance I run it on "FileScan.jar /boot"
> Program says it isn't a valid directory, but f I run "FileScan.jar /"
> Doesn't give error of not a directory, Java gives this error.
>
> Exception in thread "main" java.lang.NullPointerException
> at java.io.File.File(java.lang.String, java.lang.String)
> (/usr/lib/libgcj.so.
> 5.0.0)
> at MyFileStructure.build(java.io.File) (Unknown Source)
> at MyFileStructure.build(java.io.File) (Unknown Source)
> at MyFileStructure.build(java.io.File) (Unknown Source)
> at MyFileStructure.build(java.io.File) (Unknown Source)
> at MyFileStructure.build(java.io.File) (Unknown Source)
> at MyFileStructure.build() (Unknown Source)
> at Main.main(java.lang.String[]) (Unknown Source)
>
> private FileInfo build(File f) {
> if (f.getPath().indexOf("System Volume Information")!= -1) return
> null;
> if (!f.exists()) return null;
> if (!f.isDirectory()) return null;
>
> // f is an existing directory
> String path = f.getPath();
> String name = f.getName();
> //FileInfo mdir = new FileInfo(path, name);
>
> // Loop thru files and directories in this path
> String[] files = f.list();
> for (int i = 0; i < files.length; i++) {
> File f2 = new File(path, files[i]);
> if (f2.isFile()) {
> //mdir.addFile(new FileInfo(path, files[i],f2.length()));
> this.addFile(new FileInfo((path + "\\"), files[i],f2.length()));
> } else if (f2.isDirectory()) {
> File f3 = new File(path, files[i]);
> FileInfo m = build(f3); // recursive call
> //if (m != null) { mdir.addDir(m); }
> }
> }
> return mdir;
> }
>
>
>
> Thanks you very much
UNIX has a thing called "security", something which Windows hasn't really
got to grips with yet.
If you don't have read permission on a directory you can't read the
contents, and the line String[] files = f.list(); will return null. You
blithely go on to use files.length on the next line without bothering to
test whether files is null.
--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@xxxxxxxxxxxx
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
.
- References:
- File Scanning on a Unix Box
- From: Rishi Dhupar
- File Scanning on a Unix Box
- Prev by Date: Re: ImageProcessing Group
- Next by Date: Re: Console to GUI
- Previous by thread: Re: File Scanning on a Unix Box
- Next by thread: Cannot create an instance of Location.Criteria class - lapi.jar MIDP 2.0 J2ME
- Index(es):
Relevant Pages
|