Java tips: Java implementation file monitoring skills sharing

Source: Internet
Author: User

Java itself does not directly listen to the system's file operation events, but you can first write the C + + call operating system API listening file, and then through the method of JNI call implementation. Limited to my C + + level, the interface is not implemented in C/C + +, and there are open source components jnotify implemented This feature, this example uses Jnotify.

public class MainFrame extends JFrame ... {

Private JPanel ContentPane;

Private JTextField TextField;

Private JTextArea TextArea;

public static void Main (string[] args) ... {

Eventqueue.invokelater (New Runnable () ... {

public void run () ... {

Try ... {

MainFrame frame = new MainFrame ();

Frame.setvisible (TRUE);

} catch (Exception e) ... {

E.printstacktrace ();

}

}

});

}

Public MainFrame () ... {

Setdefaultcloseoperation (Jframe.exit_on_close);

SetBounds (100, 100, 543, 300);

ContentPane = new JPanel ();

Contentpane.setborder (New Emptyborder (5, 5, 5, 5));

Setcontentpane (ContentPane);

Contentpane.setlayout (NULL);

JLabel label = new JLabel (monitoring path:);

Label.setbounds (33, 20, 65, 15);

Contentpane.add (label);

TextField = new JTextField (d:/);

Textfield.setbounds (90, 16, 219, 21);

Contentpane.add (TextField);

Textfield.setcolumns (10);

JButton button = new JButton (start monitoring);

Button.addactionlistener (new ActionListener () ... {

public void actionperformed (ActionEvent e) ... {

Try ... {

Addwatch ();

} catch (Exception ex) ... {

Ex.printstacktrace ();

}

}

});

Button.setbounds (319, 16, 93, 23);

Contentpane.add (button);

TextArea = new JTextArea ();

JScrollPane ScrollPane = new JScrollPane (TextArea);

Scrollpane.setbounds (33, 45, 480, 207);

Contentpane.add (ScrollPane);

}

public void Addwatch () throws Exception ... {

String path = Textfield.gettext ();

int mask = jnotify.file_created | jnotify.file_deleted

| jnotify.file_modified | jnotify.file_renamed;

Boolean watchsubtree = true;

Add File Listener

int watchid = Jnotify.addwatch (path, mask, Watchsubtree, New Listener ());

}

Class Listener implements Jnotifylistener ... {

public void filerenamed (int wd, String RootPath, String oldname,

String newName) ... {

Textarea.append (file: + RootPath +: + oldname + renamed to:

+ NewName + \ n);

}

public void filemodified (int wd, string RootPath, string name) ... {

Textarea.append (file modification + RootPath +: + name + \ n);

}

public void filedeleted (int wd, string RootPath, string name) ... {

Textarea.append (delete file: + RootPath +: + name + \ n);

}

public void filecreated (int wd, string RootPath, string name) ... {

Textarea.append (New file: + RootPath +: + name + \ n);

}

}

}


Run Effect: Create a new file and modify the filename action on the D drive.

autobiography http://www.biyinjishi.com/products/a65-b6550/d100148/
individual out of the book http://www.biyinjishi.com/products/a65-b6580/d100144
Book http://www.biyinjishi.com/products/a65-b6580/d100146/
Micro bo book http://www.biyinjishi.com/products/a65-b6580/d100147/
genealogy http://www.biyinjishi.com/products/a65-b6580/d100149/
Logo Design http://www.biyinjishi.com/products/a70-b7010/
Business Card Design http://www.biyinjishi.com/products/a70-b7015/
Leaflet Design http://www.biyinjishi.com/products/a70-b7020/
Propaganda album design http://www.biyinjishi.com/products/a70-b7025/
Repair film Color http://www.biyinjishi.com/products/a70-b7050/
Typing input http://www.biyinjishi.com/products/a70-b7060/
Document Snapshot http://www.biyinjishi.com/products/a99-b9910/
send and receive fax http://www.biyinjishi.com/products/a99-b9915/
binding and binding http://www.biyinjishi.com/products/a99-b9920/
Lettering Engraved http://www.biyinjishi.com/products/a99-b9925/
Disc Burning http://www.biyinjishi.com/products/a99-b9960/

Java tips: Java implementation file monitoring skills sharing

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.