Google Answers Logo
View Question
 
Q: Browsing filesystem in a Mac OS using Java ( Answered 4 out of 5 stars,   0 Comments )
Question  
Subject: Browsing filesystem in a Mac OS using Java
Category: Computers > Programming
Asked by: kartik1801-ga
List Price: $10.00
Posted: 02 Oct 2003 12:31 PDT
Expires: 01 Nov 2003 11:31 PST
Question ID: 262243
Hi,

We have developed a digital asset management system that requires
files to be uploaded from a client machine to a central repository
server where they are processed. I developed the client interface
using Java that basically browses through a client machine's local
file system so the user can select the desired files and then upload
them. So far we have had only Windows based customers so browsing the
local filesystem has never been an issue. But now we are starting to
get some Mac customers and I would like to exploit the cross platform
inter operability of Java. Pursuant to this, I have a couple of
questions :

1. How can I get a directory listing of all the files in a Mac? I know
that the file structure in a Mac is significantly different than in a
PC so I would like to get some insight on how to do this and some code
examples if available.

2. Is there any way I can implement a file filter on a Mac? I am aware
that Mac files dont have extensions and the file type information is
stored in the header. I wanted to know if anyone out there would know
how to extract that information.

Thanks a bunch for your help,
Kartik
Answer  
Subject: Re: Browsing filesystem in a Mac OS using Java
Answered By: maniac-ga on 02 Oct 2003 20:18 PDT
Rated:4 out of 5 stars
 
Hello Kartik1801,

I assume you are using java.io.FileDialog in your existing application
"so the user can select the desired files...". If this is not correct
- use a clarification request that describes your method of file
selection so I can provide a more complete answer.

Perhaps the easiest way to do what you want is to use one (or both)
packages from Informagen
  http://www.informagen.com/java/FilenameFilter/index.html
there are two packages here
  FilterByMacOSType - filters files by type / creator [Classic Mac]
  FilterBySuffix - filters file by extension [Mac OS X]
I tag these as "classic" and "OS X", but after reviewing the code - I
don't see anything specific that would prevent you from using either
on both OS versions. These work to implement java.io.FilenameFilter
used by java.io.FileDialog.

There are also code fragments that describe how to set up / call these
packages. The specific example is for images (GIF, JPEG), but can be
adapted for other file types as well. If you don't know the Mac file
types you need - let me know - I can hunt those down for you as well.

A few other references that may help
 [1] Your First Cup of Java (for Mac OS)
  http://java.sun.com/docs/books/tutorial/getStarted/cupojava/mac.html
which describes a simple application, getting the tools for the Mac,
etc.

 [2] Class JFileChooser (if using Swing)
  http://java.sun.com/j2se/1.3/docs/api/javax/swing/JFileChooser.html
I was pointed here by another page indicating that the FileDialog
package did not implement file filters on Windows. The packages above
may work with the file filter here as well - I can't be sure.

 [3] Building the Image File Filter
  http://developer.apple.com/java/javatutorial/imagenamefilter.html
An apple site showing the use of file extensions (instead of file
types) to filter files. Part of a larger java tutorial.

  [4] A Bundle of Java
  http://www.mactech.com/articles/mactech/Vol.15/15.08/ABundleofJava/
A complete application developed for the Mac in java. Includes a
description of how the data fork and resource fork are used, the
creator code (application id), and file types. Includes some low level
calls to set up files w/ the appropriate settings for both.

Search phrases for more information include
  macintosh java file dialog
  macintosh java file dialog "creator code"
  macintosh java file dialog "file type"

This should get you on the road to satisfying your Mac users. Don't
hesitate to request clarification if some part of this is unclear or
you need further information.

  --Maniac

Request for Answer Clarification by kartik1801-ga on 08 Oct 2003 12:51 PDT
well basically I'm using the java.io.File class - I have implemented a
windows explorer style interface where a drive scan is done using the
filesystemview's isDrive class and then based on the drive letter I
use the listFiles method in the File class to list files in the drive.
I call this file list function every time i double click on a folder.
How would I do this on  a Mac?

Clarification of Answer by maniac-ga on 08 Oct 2003 15:28 PDT
Hello Kartik1801, 

Well, first - you don't have any "drive letters" on the Macintosh. For
Mac OS X systems, your file names can be handled just like any other
Unix system. For example
 / refers to the root of the file system
There are a few conventions that are a little different than most Unix
systems. For example:
 /Volumes/My Disk
will refer to a drive named "My Disk" (note the name can be long, have
spaces, etc.). The user directories are normally in
 /Users/
and the folders / files in the top level directory will be often be in
mixed case (usually Unix top level directories are always in lower
case).

As far as I can tell - all the operations you are familiar with
  listFiles()
  listFiles(FileFilter filter)  
  listFiles(FilenameFilter filter)
  FileSystemView.isDrive(File dir)
and so on are available. For example, the apple developer site lists:
  http://developer.apple.com/documentation/Java/Conceptual/Java141Development/Core_APIs/index.html
which states that the java core API's work pretty much unchanged and I
can't find any information to say otherwise.

As a side comment
  http://www.chinalinuxpub.com/doc/oreillybookself/java/exp/ch08_02.htm
has some good suggestions related to making applications portable
between the Mac and Windows - for example, use File.separator or
File.separatorChar to get the slash (forward or backward) to separate
the directory / file name. As another example, use the isDirectory()
to ensure you are dealing with a directory.

The only drawback I can see from what you describe is that it does not
necessarily "look right" when compared with other Mac applications.
You describe a windows explorer like interface - that might look odd
to a Mac user that expects to see the standard file dialog interface.

  --Maniac
kartik1801-ga rated this answer:4 out of 5 stars and gave an additional tip of: $2.00
good answer - with the info provided I could figure out the rest on my
own. thanks for the help.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy