📄 extractworddocument.java
字号:
package wordextractor;//------------------------------------------------------------------------------/** * Title: ECTools P2P * Description: 读取WORD文件内容 * Copyright: Copyright (c) 2003 * Company: 浙江大学现代制造工程研究所 * @author 战洪飞 * @version 1.0 *///------------------------------------------------------------------------------import java.io.*;import javax.swing.*;import org.textmining.text.extraction.WordExtractor;import java.awt.*;import javax.swing.border.*;import java.awt.event.*;import staticdata.*;//------------------------------------------------------------------------------public class ExtractWordDocument extends JFrame { private int length=-1; private String text=""; JScrollPane jScrollPane1 = new JScrollPane(); JTextArea contentTextArea = new JTextArea(); TitledBorder contentTitledBorder; BorderLayout borderLayout1 = new BorderLayout(); JPanel jPanel1 = new JPanel(); JLabel addressLabel = new JLabel(); BorderLayout borderLayout2 = new BorderLayout(); JPanel jPanel2 = new JPanel(); JTextField locationTextField = new JTextField(); GridLayout gridLayout1 = new GridLayout(); JButton openButton = new JButton(); JButton selectButton = new JButton(); TitledBorder titledBorder1;//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:构造函数 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param null * @return 没有返回值(null) * @exception 没有异常 */ public ExtractWordDocument() { try {jbInit(); this.setTitle("读取WORD文件中的文本内容"); this.setSize(500,500); this.setLocation(200,200); this.setVisible(true); this.locationTextField.requestFocus(); } catch(Exception ee) {StaticData.writeToLogFile("ExtractWordDocument:"+ee.toString());} }//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:测试用启动函数 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param arg 命令行参数 * @return 没有返回值(null) * @exception 没有异常 */ public static void main(String args[]){ try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(Exception ee) {StaticData.writeToLogFile("ExtractWordDocument:"+ee.toString());} new ExtractWordDocument(); }//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:读取指定文件 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param file 指定的文件 * @return 没有返回值(null) * @exception 没有异常 */ public void extractSelectedWordDocument(File file){ try{FileInputStream in = new FileInputStream (file); WordExtractor extractor = new WordExtractor(); text = extractor.extractText(in); length = text.length(); } catch(Exception ee){StaticData.writeToLogFile("ExtractWordDocumnet:"+ee.toString());} }//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:取得读取到的内容的长度 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param null * @return 没有返回值(null) * @exception 没有异常 */ public int getFileLength(){ return length; }//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:取得读取到的内容 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param null * @return 没有返回值(null) * @exception 没有异常 */ public String getFileContent(){ return text; }//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:界面布局 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param null * @return 没有返回值(null) * @exception 没有异常 */ private void jbInit() throws Exception { contentTitledBorder = new TitledBorder(""); titledBorder1 = new TitledBorder(""); jScrollPane1.setBorder(contentTitledBorder); this.getContentPane().setLayout(borderLayout1); addressLabel.setText("地址:"); jPanel1.setLayout(borderLayout2); locationTextField.addKeyListener(new java.awt.event.KeyAdapter() {public void keyPressed(KeyEvent e) {locationTextField_keyPressed(e);}}); jPanel2.setLayout(gridLayout1); openButton.setBorder(BorderFactory.createEtchedBorder()); openButton.setText("打开"); openButton.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {openButton_actionPerformed(e);}}); selectButton.setBorder(BorderFactory.createEtchedBorder()); selectButton.setText("选取..."); selectButton.addActionListener(new java.awt.event.ActionListener() {public void actionPerformed(ActionEvent e) {selectButton_actionPerformed(e);}}); contentTextArea.setBorder(BorderFactory.createEtchedBorder()); contentTextArea.setColumns(30); contentTextArea.setLineWrap(true); contentTitledBorder.setTitle("文件内容"); jPanel1.setBorder(titledBorder1); borderLayout2.setHgap(3); this.getContentPane().add(jScrollPane1, BorderLayout.CENTER); jScrollPane1.getViewport().add(contentTextArea, null); this.getContentPane().add(jPanel1, BorderLayout.NORTH); jPanel1.add(addressLabel, BorderLayout.WEST); jPanel1.add(jPanel2, BorderLayout.EAST); jPanel2.add(openButton, null); jPanel2.add(selectButton, null); jPanel1.add(locationTextField, BorderLayout.CENTER); }//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:开始读取 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param null * @return 没有返回值(null) * @exception 没有异常 */ void openButton_actionPerformed(ActionEvent e) { try{String filename=this.locationTextField.getText().trim(); if(filename.equals("")){this.contentTextArea.setText("文件名为空"); return;} else {File file=new File(filename); if(!file.exists()){this.contentTextArea.setText("文件不存在"); return;} else {this.extractSelectedWordDocument(file); this.contentTextArea.setText(text); } } } catch(Exception ee){StaticData.writeToLogFile("ExtractWordDocumnet:"+ee.toString());} }//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:开始读取选中的文件 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param null * @return 没有返回值(null) * @exception 没有异常 */ void selectButton_actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); ExampleFileFilter filter = new ExampleFileFilter(); filter.addExtension("doc"); filter.setDescription("Word Document"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(this); if(returnVal == JFileChooser.APPROVE_OPTION) {File file=chooser.getSelectedFile(); String filename=file.getName(); this.locationTextField.setText(filename); this.extractSelectedWordDocument(file); this.contentTextArea.setText(text); } }//------------------------------------------------------------------------------ /** * 2002年11月24日12:30 PM by 战洪飞 * 用处:开始读取 * @author 战洪飞 * @since 2002年11月24日12:30 PM by 战洪飞 * @param null * @return 没有返回值(null) * @exception 没有异常 */ void locationTextField_keyPressed(KeyEvent e) { int code=e.getKeyCode(); if(code==KeyEvent.VK_ENTER){openButton_actionPerformed(null);} }//------------------------------------------------------------------------------}//end of class//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -