Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- public class FileProcessingDemo {
- public static void main(String[] args) {
- // Create the frame
- JFrame frame = new JFrame("File Processing");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(500, 300);
- frame.setLayout(new FlowLayout());
- // Create the "Pick File" button
- JButton pickFileButton = new JButton("Pick File");
- JTextField filePathField = new JTextField(30);
- filePathField.setEditable(false);
- // Create the "Start Processing" button
- JButton startProcessingButton = new JButton("Start Processing");
- // Create the text field to display the key generation result
- JTextField keyGenerateField = new JTextField(30);
- keyGenerateField.setEditable(false);
- // Add action listener to the "Pick File" button
- pickFileButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- // Create a file chooser
- JFileChooser fileChooser = new JFileChooser();
- // Show the open dialog
- int result = fileChooser.showOpenDialog(null);
- // Check if a file was selected
- if (result == JFileChooser.APPROVE_OPTION) {
- // Get the selected file
- String selectedFilePath = fileChooser.getSelectedFile().getAbsolutePath();
- filePathField.setText(selectedFilePath);
- } else {
- // Display a message if no file was selected
- JOptionPane.showMessageDialog(frame, "No file selected");
- }
- }
- });
- // Add action listener to the "Start Processing" button
- startProcessingButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- String filePath = filePathField.getText();
- if (!filePath.isEmpty()) {
- // Create a new thread to process the file
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- // Use FileReader and BufferedReader to read the file
- FileReader fileReader = new FileReader(filePath);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line;
- int passwordCount = 0;
- int bitp3123Count = 0;
- // Read the file line by line
- while ((line = bufferedReader.readLine()) != null) {
- if (line.contains("password")) {
- passwordCount++;
- }
- if (line.contains("bitp3123")) {
- bitp3123Count++;
- }
- }
- bufferedReader.close();
- // Calculate the result
- int result = passwordCount * bitp3123Count;
- // Update the keyGenerateField with the result
- SwingUtilities.invokeLater(new Runnable() {
- @Override
- public void run() {
- keyGenerateField.setText(String.valueOf(result));
- }
- });
- } catch (IOException ex) {
- ex.printStackTrace();
- JOptionPane.showMessageDialog(frame, "Error reading file: " + ex.getMessage());
- }
- }
- }).start();
- } else {
- JOptionPane.showMessageDialog(frame, "Please select a file first.");
- }
- }
- });
- // Add components to the frame
- frame.add(pickFileButton);
- frame.add(filePathField);
- frame.add(startProcessingButton);
- frame.add(keyGenerateField);
- // Make the frame visible
- frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement