Duplicate :
|
Name: nt126004 Date: 12/06/2001 java version "1.4.0-beta3" Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84) Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode) Sometimes it is desirable to create a new file with all the attributes (timestamps, DOS attributes on Win32, creator/type on MacOS, mode/owner/group on Unix) of some other file. For instance, this is necessary when making a copy of a file. It is also important for doing save operations in many applications, where the document on disk is likely to be destroyed if the write gets interrupted before complete (because the original document data was erased, and writing the new document data isn't finished yet, so the document -- both the version before the save, as well as the version being saved -- is completely destroyed). The usual solution to this is to create a new file, write the new document data into it, and then File.rename() it over the original file. In both situations, the attributes of the original file are lost when the new file is created. I suggest a (semi-)opaque object class be created which represents file attributes on the native platform. That way, when a new file is to be created with the same attributes as the old file, the attributes object is fetched from the old file and applied to the new file, thus preserving the attributes of the old file. The code I envision is like this: --- BEGIN SAMPLE CODE SNIPPET --- File oldFile, newFile; // initialized elsewhere File.Attributes attr = oldFile.getAttributes(); newFile.createNewFile(attr); // create the new file with these attributes --- END SAMPLE CODE SNIPPET --- Or this: --- BEGIN SAMPLE CODE SNIPPET --- File oldFile, newFile; // initialized elsewhere File.Attributes attr = oldFile.getAttributes(); newFile.setAttributes(attr); // change newFile's attributes to match oldFile's --- END SAMPLE CODE SNIPPET --- Note that the attributes themselves (eg, the mode/owner/group on Unix systems) should be accessible to applications. Though the exact attributes available would obviously vary from platform to platform (and possibly filesystem to filesystem), programmers may wish to manipulate them anyway (probably as a special case -- "If we are running on a Unix machine, set the file's mode appropriately"). To extend on the above examples: --- BEGIN SAMPLE CODE SNIPPET --- File f; // initialized elsewhere File.Attributes attr = f.getAttributes(); if (attr instanceof File.UnixAttributes) ((File.UnixAttributes) attr).setUnixMode(0400); else if (attr instanceof File.Win32Attributes) ((File.Win32Attributes) attr).addWin32Attribute(File.Win32Attributes.READ_ONLY); --- END SAMPLE CODE SNIPPET --- (Review ID: 136728) ======================================================================