📄 encoderdecoder.java
字号:
gbc.gridy = 22;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
decodeArea = new JTextArea(7, 44);
decodeArea.setEditable(false);
decodeArea.setLineWrap(true);
decodeArea.setWrapStyleWord(true);
decodeArea.setFont(new Font("Verdana",Font.PLAIN,12));
decodePane = new JScrollPane(decodeArea);
getContentPane().add(decodePane, gbc);
/* Initializes and adds a separator to the 0, 23, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 23;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
getContentPane().add(new JSeparator(), gbc);
/* Initializes the closeButton object and adds to it to the 0, 24, 3, 1 position with CENTER alignment. */
gbc.gridx = 0;
gbc.gridy = 24;
gbc.gridwidth = 3;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
closeButton = new JButton(" Close ");
closeButton.addActionListener(this);
getContentPane().add(closeButton, gbc);
/*
addWindowListener - It contains a windowClosing() method.
windowClosing: It is called when the end user clicks the cancel button of the Window.
It closes the main window.
Parameter: we- Represents the object of the WindowEvent class.
Return Value: NA
*/
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
/* Initializes the object of the EncodingSchemes class. */
encScheme = new EncodingSchemes(this);
}
/*
keyTyped() - This method is called when the end user enters any text in
the Encoded Text text area.
Parameters: ke - Represent an object of the KeyEvent class that contains
the details of the event.
Return Value: NA
*/
public void keyTyped(KeyEvent ke)
{
if(ke.getSource()== textArea)
{
/* Enables the Encode button. */
encodeButton.setEnabled(true);
}
}
/* Defines the abstract methods defined in the KeyListener interface. */
public void keyPressed(KeyEvent ke){}
public void keyReleased(KeyEvent ke){}
/*
actionPerformed() - This method is called when the end user clicks the any button.
Parameters: ev – Represents an object of the ActionEvent class that contains
the details of the event.
Return Value: NA
*/
public void actionPerformed(ActionEvent ev)
{
/* This section is executed when the end user clicks the Select Encoding Schemes button. */
if(ev.getSource() == selectButton)
{
/* Sets the visibility of the Encoding Schemes frame to true. */
encScheme.setVisible(true);
}
/* This section is executed when the end user clicks the Browse button. */
else if(ev.getSource() == browseButton)
{
selectText.setText("");
try
{
/* Initializes the object of the JFileChooser class. */
jfc = new JFileChooser();
/* Sets the file selection mode to FILES_ONLY. */
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
/* Creates and sets the new file filter for selecting the text files*/
jfc.setFileFilter(new javax.swing.filechooser.FileFilter()
{
/* accept() - This method allows the user to select only text files
from the file chooser dialog box.
Parameter: f - Represents a file object.
Return Value: boolean
*/
public boolean accept(File f)
{
if (f.isDirectory())
{
return true;
}
/* Returns the file name */
String name = f.getName();
/* Checks whether the file name contains .txt or not */
if (name.endsWith(".txt") || name.endsWith(".TXT"))
{
return true;
}
return false;
}
/* getDescription() - This method sets the file description. ]
Parameter - NA
Returns Value - String
*/
public String getDescription()
{
return ".txt";
}
});
/* Displays the Open dialog box. */
jfc.showOpenDialog(this);
/* Gets the file from the selected location. */
file = jfc.getSelectedFile();
if(file==null)
{
}
else
{
/* Checks the file object is a File type or Directory type. */
if(file.isFile())
{
/* Gets the absolute path of the file. */
str = file.getAbsolutePath();
/* Sets the string of absolute path to the Select File to Encode text field. */
selectText.setText(str);
/* Calls the open() method. */
open();
}
else
{
/* Displays an Error message dialog box. */
JOptionPane.showMessageDialog(null, "You have selected an invalid file format!", "Error",JOptionPane.WARNING_MESSAGE);
}
}
}
catch(Exception e)
{
System.out.println("Error" + e);
}
}
/* This section is executed when the end user clicks the Encode button. */
else if(ev.getSource() == encodeButton)
{
if (textArea.getText().equals(""))
{
/* Displays an Error message dialog box. */
JOptionPane.showMessageDialog(this,"You have selected an empty file!",
"Error", JOptionPane.WARNING_MESSAGE);
}
else
{
/* Calls the encode() method. */
encode();
/* Enables the Decode button. */
decodeButton.setEnabled(true);
/* Disables the Encode button. */
encodeButton.setEnabled(false);
}
}
/* This section is executed when the end user clicks the Decode button. */
else if(ev.getSource()==decodeButton)
{
if (encodeArea.getText().equals(""))
{
/* Displays an Error dialog box. */
JOptionPane.showMessageDialog(this, "The file has no content",
"Select another File", JOptionPane.WARNING_MESSAGE);
}
else
{
/* Calls the decode() method. */
decode();
/* Enables the Encode button. */
encodeButton.setEnabled(true);
/* Disables the Decode button. */
decodeButton.setEnabled(false);
}
}
/* This section is executed when the end user clicks the Reset button. */
else if(ev.getSource() == resetButton)
{
if(textArea.getText().equals("") && encodeArea.getText().equals("")
&& decodeArea.getText().equals("") &&
selectText.getText().equals(""))
{
}
else
{
/* Clears all the text areas, text field, byte buffer and char buffer. */
textArea.setText("");
encodeArea.setText("");
decodeArea.setText("");
selectText.setText("");
buff_in.clear();
bbuf.clear();
cbuf.clear();
/* Disables the Encode button. */
encodeButton.setEnabled(false);
/* Disables the Decode button. */
decodeButton.setEnabled(false);
}
}
/* This section is executed when the end user clicks the Close button. */
else if(ev.getSource()==closeButton)
{
System.exit(0);
}
}
/*
open() - This method is invoked when the end user clicks the Browse button to open the file.
Parameter - NA
Return Value - NA
*/
public void open()
{
try
{
/* Initializes the object of the FileInputStream class. */
fin = new FileInputStream(file);
/* Gets the file channel from the file input stream. */
fchan = fin.getChannel();
/* Gets the size of the file. */
fsize = fchan.size();
/* Allocates the size of the Buffer. */
buff_in = ByteBuffer.allocate((int)fsize);
/* Reads the buffer from the channel. */
fchan.read(buff_in);
/* Rewinds the data in the buffer. */
buff_in.rewind();
/* Stores the contents of the buff_in buffer to a string. */
file_text = new String(buff_in.array());
if(file_text.equals(""))
{
/* Displays an Error dialog box. */
JOptionPane.showMessageDialog(null, "The file has no content",
"Select another File", JOptionPane.WARNING_MESSAGE);
}
else
{
/* Displays the content stored in the file_text string into Encoded Text text area. */
textArea.setText(file_text);
/* Enables the Encode button. */
encodeButton.setEnabled(true);
}
/* Closes the file channel. */
fchan.close();
/* Closes the file input stream. */
fin.close();
}
catch(IOException ioe)
{
System.err.println("I/O Error on Open");
}
catch(UnsupportedCharsetException e)
{
System.err.println("The File cannot be encoded");
}
}
/*
encode() - This method is invoked when the end user clicks the Encode
button to encode the specified text or a file.
Parameter - NA
Return Value - NA
*/
public void encode()
{
try
{
/* Initializes the object of the Charset class and sets the encoding scheme. */
charset = Charset.forName(EncodingSchemes.encodingScheme);
/* Initializes the object of the CharsetEncoder class. */
encoder = charset.newEncoder();
/* Gets the text from the Encoded Text text area. */
String str = textArea.getText();
/* Encodes the text and stores the encoded text in the byte buffer. */
bbuf = encoder.encode(CharBuffer.wrap(str));
buff_out = encoder.encode(CharBuffer.wrap(str));
/* Creates and initializes the object of the StringBuffer class. */
StringBuffer sb = new StringBuffer();
for (int j = 0; buff_out.hasRemaining(); j++)
{
/* Converts the bytes into hexadecimal string. */
int b = buff_out.get();
int ival = ((int) b) & 0xff;
char c = (char) ival;
sb.append(Integer.toHexString (ival));
}
String content = sb.toString();
/* Clears the buff_out byte buffer. */
buff_out.clear();
/* Displays the encoded text in the Encoded Text text area. */
encodeArea.setText(content);
}
catch(Exception e)
{
System.out.println(e);
}
}
/*
decode() - This method is invoked when the end user clicks the Decode
button to decode the encoded text into readable format.
Parameter - NA
Return Value - NA
*/
public void decode()
{
try
{
/* Initializes the object of the CharsetDecoder class. */
decoder = charset.newDecoder();
/* Decodes the encoded text into a readable form and stores it into a
character buffer. */
cbuf = decoder.decode(bbuf);
/* Converts the data stored in cbuf to a string. */
String s = cbuf.toString();
/* Displays the decoded text into the Decoded Text text area. */
decodeArea.setText(s);
/* Clears the byte and char buffers. */
bbuf.clear();
cbuf.clear();
}
catch(Exception e)
{
System.out.println(e);
}
}
/*
main() - This method creates the main window of the user interface and displays it.
Parameters:
args[] - Contains any command line arguments passed.
Return Value: NA
*/
public static void main(String[] args)
{
try
{
/* Sets the look and feel of the Encoder/Decoder application to window look and feel. */
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch(Exception e)
{
System.out.println("Unknown Look and Feel." + e);
}
/* Creates and initializes the object of the EncoderDecoder class. */
EncoderDecoder ed = new EncoderDecoder();
/* Displays the main window of the Encoder/Decoder application. */
ed.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -