⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileprocessor.java

📁 可以上传本地文件
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//* * Fileprocessor.java * * Created on 2009-2-22, 2:53:41 */package fi.mamk.fileprocessor;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.util.regex.Matcher;import java.util.regex.Pattern;import javax.swing.JFileChooser;/** * * @author Sun Wen(A5250) * I add a replace funtion. */public class Fileprocessor extends javax.swing.JFrame {    File file;    Pattern p;    Matcher m;    /** Creates new form Fileprocessor */    public Fileprocessor() {        initComponents();    }    /** This method is called from within the constructor to     * initialize the form.     * WARNING: Do NOT modify this code. The content of this method is     * always regenerated by the Form Editor.     */    @SuppressWarnings("unchecked")    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents    private void initComponents() {        jFileChooser1 = new javax.swing.JFileChooser();        select = new javax.swing.JButton();        search = new javax.swing.JButton();        jLabel1 = new javax.swing.JLabel();        jLabel2 = new javax.swing.JLabel();        filename = new java.awt.TextArea();        text = new java.awt.TextArea();        output = new java.awt.TextArea();        jLabel3 = new javax.swing.JLabel();        replacetext = new java.awt.TextArea();        replace = new javax.swing.JButton();        jScrollPane1 = new javax.swing.JScrollPane();        textoutput = new javax.swing.JTextArea();        jScrollPane2 = new javax.swing.JScrollPane();        replaceoutput = new javax.swing.JTextArea();        jLabel4 = new javax.swing.JLabel();        jLabel5 = new javax.swing.JLabel();        jLabel6 = new javax.swing.JLabel();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        setTitle("File Processor");        select.setText("Select File");        select.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                selectActionPerformed(evt);            }        });        search.setText("Search");        search.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                searchActionPerformed(evt);            }        });        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N        jLabel1.setText("Currently select a file");        jLabel2.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N        jLabel2.setText("Write text to search");        jLabel3.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N        jLabel3.setText("Write text to replace");        replace.setText("Replace");        replace.addActionListener(new java.awt.event.ActionListener() {            public void actionPerformed(java.awt.event.ActionEvent evt) {                replaceActionPerformed(evt);            }        });        textoutput.setColumns(20);        textoutput.setRows(5);        jScrollPane1.setViewportView(textoutput);        replaceoutput.setColumns(20);        replaceoutput.setRows(5);        jScrollPane2.setViewportView(replaceoutput);        jLabel4.setText("Replaced");        jLabel5.setText("Original");        jLabel6.setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N        jLabel6.setText("Result");        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());        getContentPane().setLayout(layout);        layout.setHorizontalGroup(            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)            .add(layout.createSequentialGroup()                .addContainerGap()                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()                        .add(filename, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)                        .addContainerGap())                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)                            .add(layout.createSequentialGroup()                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)                                    .add(select)                                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)                                    .add(search, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                                    .add(text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 239, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))                            .add(layout.createSequentialGroup()                                .add(jLabel2)                                .add(111, 111, 111)))                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)                            .add(jLabel3)                            .add(replace, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 96, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                            .add(replacetext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 240, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))                        .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))                    .add(layout.createSequentialGroup()                        .add(jLabel4)                        .addContainerGap(451, Short.MAX_VALUE))                    .add(layout.createSequentialGroup()                        .add(jLabel6)                        .addContainerGap(463, Short.MAX_VALUE))                    .add(layout.createSequentialGroup()                        .add(output, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)                        .addContainerGap())                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)                            .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE))                        .addContainerGap())                    .add(layout.createSequentialGroup()                        .add(jLabel5)                        .addContainerGap(451, Short.MAX_VALUE))))        );        layout.setVerticalGroup(            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)            .add(layout.createSequentialGroup()                .addContainerGap()                .add(select)                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)                .add(jLabel1)                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                .add(filename, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)                    .add(jLabel2)                    .add(jLabel3))                .add(10, 10, 10)                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)                    .add(text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                    .add(replacetext, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)                    .add(search)                    .add(replace))                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)                .add(jLabel6)                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                .add(output, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                .add(18, 18, 18)                .add(jLabel5)                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)                .add(jLabel4)                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 185, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)                .addContainerGap())        );        pack();    }// </editor-fold>//GEN-END:initComponents    private void selectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectActionPerformed        textoutput.setText("");        // Set the file chooser properties        jFileChooser1.setDialogTitle("Select a file to open");        // Show the open file dialog to the user        int returnVal = jFileChooser1.showOpenDialog(this);        if ( returnVal == JFileChooser.APPROVE_OPTION ) {            // User has approved the open file action (pressed the open button)            // Get the file information            file = jFileChooser1.getSelectedFile();            filename.append(file.getAbsolutePath());        }        // Read the contents of the selected file            try {                // Open a FileInputStream from the file                FileInputStream fis = new FileInputStream(file);                // Buffer the input stream for faster reading                BufferedReader bufReader = new BufferedReader( new InputStreamReader(fis) );                // Read the contents of the file                while( bufReader.ready() ) {                    textoutput.append( bufReader.readLine() + "\n" );                }                // Close the streams                bufReader.close();                fis.close();            } catch (FileNotFoundException ex) {                System.out.println("There was error in opening the file: " + ex.getMessage() );            } catch (IOException ex) {                System.out.println("There was error in reading the file: " + ex.getMessage() );            }    }//GEN-LAST:event_selectActionPerformed    private void searchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchActionPerformed          int count=0;        try {                output.setText("");                // Open a FileInputStream from the file                FileInputStream fis = new FileInputStream(file);                // Buffer the input stream for faster reading                BufferedReader bufReader = new BufferedReader( new InputStreamReader(fis) );                              // pattern is used for set a specific string to be a templet                // matcher is used for search the templet in a specific string                //matcher.find() is a boolean value that shows whether the string contains the pattern.                while(bufReader.ready()){                    p = Pattern.compile(text.getText());                    m = p.matcher(bufReader.readLine());                    while (m.find()){                        count++;                    }                }                               output.append("There are "+count+" "+"\'"+text.getText()+"\'"+" in this file.");                // Close the streams                bufReader.close();                fis.close();            } catch (FileNotFoundException ex) {                System.out.println("There was error in opening the file: " + ex.getMessage() );            } catch (IOException ex) {                System.out.println("There was error in reading the file: " + ex.getMessage() );            }    }//GEN-LAST:event_searchActionPerformed    private void replaceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_replaceActionPerformed        try {                replaceoutput.setText("");                // Open a FileInputStream from the file                FileInputStream fis = new FileInputStream(file);                // Buffer the input stream for faster reading                BufferedReader bufReader = new BufferedReader( new InputStreamReader(fis) );               while( bufReader.ready() ) {                        String rep=bufReader.readLine().replaceAll(text.getText(), replacetext.getText());                     replaceoutput.append( rep + "\n" );                }                                               bufReader.close();                fis.close();            } catch (FileNotFoundException ex) {                System.out.println("There was error in opening the file: " + ex.getMessage() );            } catch (IOException ex) {                System.out.println("There was error in reading the file: " + ex.getMessage() );            }}//GEN-LAST:event_replaceActionPerformed    /**    * @param args the command line arguments    */    public static void main(String args[]) {        java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {                new Fileprocessor().setVisible(true);            }        });    }    // Variables declaration - do not modify//GEN-BEGIN:variables    private java.awt.TextArea filename;    private javax.swing.JFileChooser jFileChooser1;    private javax.swing.JLabel jLabel1;    private javax.swing.JLabel jLabel2;    private javax.swing.JLabel jLabel3;    private javax.swing.JLabel jLabel4;    private javax.swing.JLabel jLabel5;    private javax.swing.JLabel jLabel6;    private javax.swing.JScrollPane jScrollPane1;    private javax.swing.JScrollPane jScrollPane2;    private java.awt.TextArea output;    private javax.swing.JButton replace;    private javax.swing.JTextArea replaceoutput;    private java.awt.TextArea replacetext;    private javax.swing.JButton search;    private javax.swing.JButton select;    private java.awt.TextArea text;    private javax.swing.JTextArea textoutput;    // End of variables declaration//GEN-END:variables}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -