File encrypital, the operation process must involve the use of the file selectioner, so the file encryption is used as an example. The following example is a file encryption that I wrote my own. There is no special encryption algorithm, only for the use of the demonstration file selectioner JFilechooser.
The encryption interface is shown in Figure:
The project directory structure is shown in the figure:
The source code of each file is posted below:
Mainform.java
package com.lidi; Import Javax.swing.*; Import Java.awt.*; Public Class Mainform Extends JFRAME { / *** Constructing interface** @Author 1109030125* / PRIV ATE Static Final Long SerialVersionuid = 1L; /* Main Window Several elements in the body*/ private jframe mainform = new jframe ("txt file encryption"); // The main window, the title "TXT file encryption" Prive Jlabel Label1 = New JLabel ("Please select files to be encrypted or decrypted : "); Private Jlabel Label2 = New Jlabel (" Please select the encrypted or decrypted file storage position: "); static JtextField TargetFile = New JTextField (); // Select the text domain of encrypted or decrypted the file path Public Static Jbutton ButtonBrowSource = New Jbutton ("Browse"); // The browse button c jbutton Buttonbrowsetarget = New Jbutton ("Browse"); // Browse button Public Static Jbutton ButtonenCrypt = New Jbutton ("Encryption"); // The encrypted button Public Static Jbutton ButtonDeCrypt = New Jbutton ("Decrypt"); ORM () {container container = mainform.getContentPane ( );/* Set the main window attribute*/ mainform.Setsize (400, 270); // Set the main window size Mainform.SetDefaultCloseoperation (WindowConstants.exit_on_Close); M.SetLocationRelativeto (null ); // Set in Mainform.SetResizable (FALSE) in the center of the screen; // Set the window that cannot be zoomed in Mainform.SetLayout (NULL); Mainform.SetVisible (True); // Display window/* Set the element location layout*/ Lab. EL1. Setbounds (30, 10, 300, 30); SourceFile.Setbounds (50, 50, 200, 30); ButtonbrowseSource.setbounds (270, 50, 60, 30); Label2.Setbounds (30, 90, 300, 300 30); TargetFile.Setbounds (50, 130, 200, 30); ButtonBrowsetarget.setbounds T.Setbounds (200, 180, 60, 30 ); /* Binding event monitoring for each element* / ButtonbrowSource.addAcityListener (New Browseaction ()); // For the source file browse button, click the button to call the file buttonbrowSetarget.a DDactionListener (New Browseaction ( ); // Browse the buttons for the target position, click the button to call the file to select the window buttonenCrypt.adDactionListener (New EncryptAction ()); // The encrypted button is bound to the monitor. Encryption and output to the target location ButtondeCrypt.adDactionListener (New DecryptAction ()); // Bind the monitor for the decryption button. umentlistener ( New TextFieldAction ()); // is a binding event for the source file text domain. If the file is .txt type, the decryption button is disabled; if it is a .kcd file, the encryption button is disabled. SourceFile.SetEditable (FALSE); // Set the source file text domain cannot manually modify the targetFile.SetEditable (false); // Set the target location of the text domain. Bel2); Container. add (sourcefile); container.add (targetFile); container.add (buttonbrowSource); container.add (buttonbrowSetarget); container.add (butonenNCrypt); container.add (ButtondeCrypt);} Public Static Void Main (String ARGS [] ) {newsform ();}}Browseaction.java
package com.lidi; Import java.awt.event.actionevent; Import Java.awt.action.ACTIONLISTENER; Import javax.swing.jfilechooser; Import Chooser.FilenameextentFilter; Public Class BrowseAction Implements ActionListener {@Override Public Void ActionPerFormed (ActionEvent E) {if (e.getsource (). Equals (Mainform.ButtonbrowSource)) {jfilechooser fcdlg = new jfilechooser (); Files to be encrypted or decrypted ... "); = New FILENAMEEXTENSIONFILTER ("Text Files (*.txt;*. KCD)", "txt", "kcd"); fcdlg.setFileFileter (Filter); Int ReturnVal = FCDLG.ShowOpendialog ( null); if (returnval == jfilechooser .AppRove_opting) {string filepath = fcdlg.getselectful (). Getpath (); Mainform.sourceFile.SetText (filepath);}} Else if (e.getsource (). Als (Mainform.ButtonbrowSetarget) {jfilechooser fcdlg = new jfilechooser (); FCDLG.SetDialogtitle ("Please select the encrypted or decrypted file storage directory"); alog (null); if (returnval == jfilechooser.approve_opting) { String filepath = fcdlg.getSelectFile (). Getpath (); mainform.targetFile.settext (filepath);}}}}}}EncryptAction.java
package com.lidi; Import java.awt.event.actionevent; Import java.awt.event.actionListener; Import java.file; Import Java.filereeder; Import ava.io.Filewriter; Import Java.io.ioException ; Import javax.swing.joptingPane; Public Class EncryptAction Implements ActionListener {@Override Public Void ActionPerformed E) {// Todo A UTO-GENERATD METHOD Stub IF (Mainform.sourceFile.getText (). Isempty ()) {jobpane.showMessageDialog (Null, "Please select the file to be encrypted!");} else if (mainform.targetFile.gettext (). Isempty ()) {jobpane.showmessageDialog (null, "Select the encrypted file storage directory!");} Else {String SourcePath = Mainform.sourceFile.getText (); String targetpath = mainform.targetFile.gettext (); ame = file.getName (); file dir = new file (targetpath); if (File.exist () && Dir.isdirectory ()) {File Result = New File (GetFinalfile (TargetPath, FILENAME)); if (! Result.exist ()) {TRY {TRY.Createnewf iLe ();} Catch ( IoException E1) {jobpane.showmessageDialog (null, "Objective file creation failed, please check whether the directory is read only! ");}} Try {Filereader FR = New FileRereader (File); Filewriter Fw = New Filewriter (Result); int ch = 0; space ((ch = Fr.read ())) {// System .out.print (Encrypt (CH)); fw.write (enCrypt (ch));} fw.close (); fr.Close (); jobpane.showmessagedialog (null, "encrypted success!"); Exception E1) {jobpane.showmessageDialog (null, "unknown error!");} Else if (! File.exist ()) {jobpane.showmessagedialog (null, " Here! ");} Else {jobpane .showmessageDedialog (null, "The file storage directory does not exist after encrypted files!");}} Public Char Encrypt (int CH) {int x = ch + 1; RETURN (char) (x);} Public String File (String targetPath , String filename) {int length = filename.Length (); string foodfilename = filename.substring (0, length -4); string finalfile = targetpath "//" + FINALF lename + ".kcd"; Return Finalfile;}}DecryptAction.java
package com.lidi; Import java.awt.event.actionevent; Import java.awt.event.actionListener; Import java.file; Import Java.filereeder; Import ava.io.Filewriter; Import Java.io.ioException ; Import javax.swing.joptingPane; Public Class DecryptAction Implements ActionListener {@Override Public Void ActionPerformed E) {// Todo A UTO-GENERATD METHOD Stub IF (Mainform.sourceFile.getText (). Isempty ()) {jobpane.showMessageDialog (NULL, "Please select the file to be decrypted!");} else if (mainform.targetFile.gettext (). Isempty ()) {jobpane.showmessageDialog (null, "Please select the decryption file storage directory!");} Else {String SourcePath = Mainform.sourceFile.getText (); String targetpath = mainform.targetFile.gettext (); ame = file.getName (); file dir = new file (targetpath); if (File.exist () && Dir.isdirectory ()) {File Result = New File (GetFinalfile (TargetPath, FILENAME)); if (! Result.exist ()) {TRY {TRY.Createnewf iLe ();} Catch ( IoException E1) {jobpane.showmessageDialog (null, "Objective file creation failed, please check whether the directory is read only! ");}} Try {Filereader FR = New FileRereader (File); Filewriter Fw = New Filewriter (Result); int ch = 0; space ((ch = Fr.read ())) {// System .out.print (Encrypt (CH)); fw.write (decrypt (ch));} fw.close (); fr.close (); jobpane.showmessagedialog (null, "Decrypt!"); Exception E1) {jobpane.showmessagedialog (null, "unknown error!");} Else if (! File.exist ()) {jobpane.showmessagedialog (null, " Here! ");} Else {jobpane .showmessageDedialog (null, "The file storage of the file storage does not exist!");}} public charg (int ch) {// double x = 0 -math.pow (ch, 2); int x = ch -1 1 ; Return (char) (x);} Public String GetFinalfile (String targetPath, String Filename) {int length = filename.length (); String FinalFilename = .substring (0, length -4); String Finalfile = TargetPath + " // " + Finalfilename +" .txt "; RETURN FINALFILE;}}TextFieldAction.java
package com.lidi; Import javax.swing.event.documentevent; Import Javax.swing.event.documeentListener; Public Class TextField Implements Listener {@Override Public Void InsertupDate (DocumentEvent E) {// Todo Auto-Generalted Mettonajust () ;} @OVERRIDE PUBLIC VOID RemoveUpdate (DocumentEvent E) {// Todo Auto-Generant Method Stub Buttonajust (); Ocumeentevent E) {// Todo Auto-Generable Method Stub Buttonajust ();} Public Void Buttonajust () {string file = mainform.sourceFile.getText (); if (file.endswith ("txt") {Mainform.ButtondeCrypt.SetEnabled (False); Mainform.Button Encrypt.setenabled (true);} if (file. Endswith ("KCD")) {Mainform.ButtonenCrypt.Setenabled (FALSE); Mainform.ButtondeCrypt.Setenabled (TRUE);}}}}The above is all the contents of this article. I hope everyone can like it.