Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
o RandomAccessFile
After you get a file channel, you can use either of two pairs of
methods that it offers for working with locks: lock and tryLock.
FileLock lock()
FileLock tryLock()
The no-argument versions of the two methods try to get an
exclusive lock on the entire file associated with the channel.
The lock method does not return until the lock is acquired. The
tryLock method returns null if another process has a region of
the file currently locked.
FileLock lock(long position, long size, boolean shared)
FileLock tryLock(long position, long size, boolean shared)
The three-argument versions let you lock a piece of a file. The
position argument is the starting position for the lock. The size
argument represents the number of bytes to lock. A shared value
of true means you want a shared lock, while a false value is for
an exclusive lock. The size argument is not limited to the size
of a file, this allows you to lock a region before you write to
the file. For instance, the three argument version of lock and
tryLock go from 0 to Long.MAX_VALUE.
All the file locking methods of FileChannel return a FileLock.
You need this to release the lock. As with thread locking,
the best approach is to keep the file locked for the smallest
amount of time. FileLock offers the following methods:
o channel - returns the source FileChannel for the lock
o isShared - reports if the lock is shared
o isValid - reports if the lock is still valid (the lock is no
longer valid if the file channel is closed)
o overlaps(long position, long size) - checks for overlap with
the given region
o position - returns the start position within locked region
o release - releases the lock
o size - returns the number of bytes within the locked region
o toString - provides a string representation of the lock
Here's a program that demonstrates the file locking features.
The program gets an exclusive lock on a file, reports when it has
the lock, and then wait until you press the Enter key. Try
running the program concurrently in two separate windows. You'll
see that the program won't run a second time until you press
Enter in the first run. (Note that if you have a file named
junk.dat in the directory in which you run this program, the file
will be overwritten.)
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
public class Locking {
public static void main(String arsg[])
throws IOException {
RandomAccessFile raf =
new RandomAccessFile("junk.dat", "rw");
Button.background
Button.border
Button.darkShadow
Button.focusInputMap
Buton.font
Button.foreground
Button.highlight
Button.light
Button.margin
Button.shadow
Button.textIconGap
Button.textShiftOffset
java.awt.*;
javax.swing.*;
javax.swing.plaf.*;
javax.swing.plaf.metal.*;
are available from third parties.) With these other look and feels
(Windows and Motif), the Swing components take on the look of
a native application for those platforms.
To change the look and feel of an application, simply pass the
class name into the setLookAndFeel method of the UIManager. You
can also ask the UIManager for the name of the look and feel for
the native platform.
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
After the look and feel has been changed, all new components
created will use the defaults associated with the new look and
feel. To change existing components, you need to invoke updateUI
for a single component or updateComponentTreeUI for a set of
components in a window.
import java.awt.*;
import javax.swing.*;
public class NativeLNF {
public static void main(String args[]) {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println("Unable to change");
}
JFrame frame = new JFrame("Native");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
Container c = frame.getContentPane();
JButton top = new JButton("Top");
c.add(top, BorderLayout.NORTH);
JButton bottom = new JButton("Bottom");
c.add(bottom, BorderLayout.SOUTH);
frame.setSize(200, 100);
frame.show();
}
}
You can create your own look and feel objects, in addition to
using the system defined look and feel objects. To create your
own objects, you need to define the complete table of lookup
settings for every available component. More simply (and more
typically), you subclass an existing look and feel, and just
customize the handful of settings you don't like. These settings
can grow until you customize all the settings. But to start, it
is easier to subclass and override the handful you need to than
start from scratch and fill in all the settings (of which there
are approximately 600). Here's an example that uses this approach.
import
import
import
import
java.awt.*;
javax.swing.*;
javax.swing.UIDefaults;
javax.swing.plaf.metal.MetalLookAndFeel;
policies:
http://www.sun.com/share/text/termsofuse.html
http://www.sun.com/privacy/
http://developer.java.sun.com/berkeley_license.html
* FEEDBACK
Comments? Send your feedback on the Core Java Technologies
Tech Tips to:
jdc-webmaster@sun.com
* SUBSCRIBE/UNSUBSCRIBE
Subscribe to other Java developer Tech Tips:
- Enterprise Java Technologies Tech Tips. Get tips on using
enterprise Java technologies and APIs, such as those in the
Java 2 Platform, Enterprise Edition (J2EE(tm)).
- Wireless Developer Tech Tips. Get tips on using wireless
Java technologies and APIs, such as those in the Java 2
Platform, Micro Edition (J2ME(tm)).
To subscribe to these and other JDC publications:
- Go to the JDC Newsletters and Publications page,
(http://developer.java.sun.com/subscription/),
choose the newsletters you want to subscribe to and click
"Update".
- To unsubscribe, go to the subscriptions page,
(http://developer.java.sun.com/subscription/),
uncheck the appropriate checkbox, and click "Update".
- To use our one-click unsubscribe facility, see the link at
the end of this email:
- ARCHIVES
You'll find the Core Java Technologies Tech Tips archives at:
http://java.sun.com/jdc/TechTips/index.html
- COPYRIGHT
Copyright 2002 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.
This document is protected by copyright. For more information, see:
http://java.sun.com/jdc/copyright.html
Core Java Technologies Tech Tips
September 24, 2002
Sun, Sun Microsystems, Java, Java Developer Connection, J2SE,
J2EE, and J2ME are trademarks or registered trademarks of Sun
Microsystems, Inc. in the United States and other countries.