Sequence of clone object, file dialog, with the progress of the input stream and file locks

I. clone sequence of the object

1. What is the sequence of the target clone?
Sometimes you want object a "copy", so that variations replica of the original object does not cause physical changes, and vice versa. We call this replica is a clone of the original object (referred to as cloning).

2. How to achieve?
Cloning using a stream of objects easily acquire a sequence of objects, only the object stream output destination point, and the destination as a source object input stream, then the object input stream read back from the source object must It is a clone of the original object. Briefly:
is the object of the current input stream to obtain a clone sequence information object by object.

When the program wants to get a faster rate clone objects, information may flow sequence with the target object is written to memory.

II. File Dialog

1. What is the file dialog?
File is a file selection dialog box interface.

2. How to use?
We can create a file dialog using JFileChooser class javax.swing package.

The following steps:
(1) using the JFileChooser class constructor (); invisible to create the initial modal dialog.
(2) then call any of the following two methods to make a dialog boxes:

showSavaDialog(Component a);

showOpenDialog(Component a);

The former provided Save the file interface, which provides open interface file. Parameter specifies a position of the dialog box is visible.
(A is null, the file dialog appears in the center of the screen, if a component is not empty, the file dialog is centered directly in front of the assembly a)

3. General Operation
"OK" on (1) the user clicks the file dialog, "Cancel" or "off" icon, the file dialog will disappear.
showSavaDilog (); or showOpenDialog (); method returns one of the following constants:
JFileChooser.APPROVE_OPTION
JFileChooser.CANCEL_OPTION.

(2) If the file types that you want the file dialog that users need, you can use this class:
FileNameExtensionFilter first create an object:
FileNameExtensionFilter filter = new new FileNameExtensionFilter ( "image file", "jpg", "gif
"); then let the file dialog call
setFileFilter (FileNameExtensionFilter filter);
method to set the default dialog box opens to display or file type as a parameter to the specified type:
Chooser.setFileFilter (filter);

3. Enter the flow with a progress bar

If the desired file is read to see progress, can use the input stream classes ProgressMonitorInputStream javax.swing package provides
its constructor is:
ProgressMonitorInputStream to (the Component c, String S, the InputStream);
front component specified in the progress bar display parameter c If c is null, display in front of the screen.

IV. File locks

In the case of practical applications, often there will be several programs working on the same file occurs, such as at the same time update or read the file. In general cause of conflict, but java provides file-locking capabilities that can help solve this problem.

1.FileLock and classes respectively FileChannel java.nio and java.nio.channels package. Input can be used to read and write file locks the file output stream, the following classes will be described in conjunction with RandomAccessFile use file locks.

RandomAccessFile can be used to create a stream file locks when reading and writing files. It does not release the lock, other programs can not operate locked files.

To use the file locks as follows:

RandomAccessFile flow setup using first stream object to the file, read-write property of the object must be RW , for example:

RandomAccessFile input=new RandomAccessFile("Example.java","rw");

The method then calls input stream the getChannel (); FileChannel obtained object (channel) is connected to the bottom of a file , for example:

FileChannel channel = input.getChannel();

TryLock call channel (); or Lock (); a method of obtaining the FileLock (file lock) objects, this process is referred to as file locking.
E.g:

FileLock lock = channel.tryLock();

After the file lock object produced, it will prohibit any program for any operation.
After the file lock on, if you want to read and write files must let FileLock object calls release (); release the file lock . E.g:

lock.release();
Published 35 original articles · won praise 0 · Views 1286

Guess you like

Origin blog.csdn.net/c1776167012/article/details/104194390