Use of JFileChooser and FileFilter, and Java randomaccessfile usage

Source: Internet
Author: User

Today to learn from a peer download source code, which met two small problems, the internet carried out Baidu, found the answer, readily recorded, so that in the future to look over.

1. First customize a file download of the main interface, including the creation of tasks, pause tasks, continue tasks, delete tasks, click to create a task need to pop up a jframe jfilechooser, file selection, and custom filters for file filtering.

JFileChooser filechooser = new JFileChooser ();//Create File Selector
Filechooser.setcurrentdirectory (New File (".")); /Set Current directory
Filechooser.setacceptallfilefilterused (FALSE);
Show All Files
Filechooser.addchoosablefilefilter (New Javax.swing.filechooser.FileFilter () {
Public Boolean accept (File f) {
return true;
}
Public String getdescription () {
Return "All Files (*. *)";
}
});
Show Java source files
Filechooser.setfilefilter (New Javax.swing.filechooser.FileFilter () {
Public Boolean accept (file f) {//sets the suffix name of the available files, which can be customized as. Ava. XML, etc.
if (F.getname (). EndsWith (". Java") | | F.isdirectory ()) {
return true;
}
return false;
}
Public String getdescription () {
Return "Java source program (*.java)";
}
});
You can use the Setfilefilter method repeatedly to set the selection type of the JFileChooser

2.Java randomaccessfile Usage

In the file download, not using InputStream and OutStream, but using the unfamiliar randomaccessfile, Internet search to find its related concepts, as well as the file read and write operation of the permission control.

Randomaccessfile is used to access files that hold data records, you can use the Seek () method to access the records and read and write them. The sizes of these records do not have to be the same, but their size and position must be knowable. However, this class is limited to manipulating files.

Randomaccessfile does not belong to the InputStream and OutputStream class systems. In fact, in addition to implementing the Datainput and DataOutput interfaces (both DataInputStream and DataOutputStream implement both interfaces), it has nothing to do with these two classes, Do not even use any of the features that already exist in the InputStream and OutputStream classes; it is a completely independent class, and all methods (most of them belong to itself) are written from scratch. This may be because randomaccessfile can move back and forth within a file, so its behavior is fundamentally different from other I/O classes. In summary, it is a separate class that inherits directly from object.

Basically, the way Randomaccessfile works is to combine DataInputStream and dataoutputstream, plus some of its own methods, such as the getfilepointer of positioning (), The Seek () to be moved in the file, and the number of bytes to determine the length (), skipbytes () of the file size skipped. In addition, its constructor has a parameter that indicates whether to open the file as read-only ("R") or read-write ("RW") (fopen () of C). It does not support write-only files.

Only Randomaccessfile has a seek search method, and this method applies only to files. Bufferedinputstream has a mark () method that you can use to set the tag (save the result in an internal variable), and then call Reset () to return to that position, but it is too weak and not practical.

The above two issues are personal reading code in the process of encountering problems, found in the online search related concepts, as well as code examples, only for personal learning to use.

Use of JFileChooser and FileFilter, and Java randomaccessfile usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.