In the file traversal dialog box in the folder, select a small program used to create the chm File.

Source: Internet
Author: User

// Traverse the folder to replace package com. mengdian. findandreplace. service; import java. io. bufferedReader; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. inputStreamReader; import java. io. outputStreamWriter; import java. io. printWriter; import javax. swing. JFrame; import javax. swing. JOptionPane; public class FindAndReplaceService {/***** @ param file * file to be replaced Folder recursive replacement * @ return * @ throws Exception */public static boolean replace (File file) throws Exception {if (file. isDirectory () {// if it is a directory, get its sub-File and replace it with file [] files = File. listFiles (); for (int I = 0; I <files. length; I ++) {replace (files [I]);} return false;} else {searchAndReplace (file); return true ;}} /***** @ param file: target file to be replaced * @ return * @ throws Exception */public static boolean searchAndRe Place (File file) throws Exception {String fileName = file. getName (); // only operate on. html files. Otherwise, directly jump out of if (! FileName. contains (". html ") {return true;} // if (fileName. contains (". jpg ") | fileName. contains (". png ") | fileName. contains (". gif ") {// return false; //} BufferedReader br = new BufferedReader (new InputStreamReader (new FileInputStream (file)," gb2312 ")); stringBuffer sb = new StringBuffer (); String str = null; while (null! = (Str = br. readLine () {sb. append (str);} String des = sb. toString (); // append the function des = des. replace ("<body> ", "<body oncontextmenu = 'Return false' ondragstart = 'Return false' leftMargin = 0 topMargin = 0 oncopy = 'Return false' oncut = 'Return false'> "); des = des. replace ("<body>", "<body oncontextmenu = 'Return false' ondragstart = 'Return false' leftMargin = 0 topMargin = 0 oncopy = 'Return false' oncut = 'Return false'> "); // adds the padding and the padding between the left and des = des to the body. replace ("leftMargin = 0", "leftMargin = 10"); des = des. replace ("topMargin = 0", "topMargin = 10"); des = des. replace (" </span> <span lang = EN-US style = 'font-size: 16.0pt; font-family: '> ICS27.100 </span> </p> <p class = MsoNormal style = 'line-height: 26w.pt'> <span lang = EN-USstyle = 'font-size: 16.0pt; font-fam Ily: '> P61 </span> </p> <p class = MsoNormal style = 'line-height: 26824pt'> <span style = 'font-size: 16.0pt; font-family: '> record filing number: <span lang = EN-US> J195 </span>-<span lang = EN-US> 2002 </span> </p> ", ""); // System. out. println (des); String result1 = des. replace ("http://www.Word2ChmOnline.com", ""); String result_des = result1.replace ("this document is generated by the DeBin software. "," "); String result_des2 = result_des.replace (" <B> <span style = 'font-family: '> For details, see system reference standard module </span> </B> "," <span style = 'font-family: '> For details, see system reference standard module </span> "); result_des2 = result_des2.replace (" <B> <span style = 'font-size: 13.5pt; font-family: '> refer to system reference standards </span> </B> "," <span style = 'font-family: '> For details, see system reference standard module </span> "); result_des2 = result_des2.replace (" <B> <span style = 'font-size: 13.5pt; font-famil Y: '> refer to System </span> </B> <span style = 'font-size: 13.5pt; font-family: '> View </span> </B> <span style = 'font-size: 13.5pt; font-family: '> Standard </span> </B> "," <span style = 'font-family: '> For details, see system reference standard module </span> "); result_des2 = result_des2.replace (" <B> <span style = 'font-size: 13.5pt; font-family: '> See </span> </B> <span style = 'font-size: 13.5pt; font-family: '> System </span> </B> <span style = 'font-size: 13.5pt; font -Family: '> standards </span> </B> "," <span style = 'font-family: '> For details, see system reference standard module </span> "); result_des2 = result_des2.replace (" <B> For details, see system reference standard module </B> ", "For details, see the system standard module"); result_des2 = result_des2.replace ("<B> <span style = 'font-family: '> For details, see system reference standard module </span> </B> "," <span style = 'font-family: '> For details, see system reference standard module </span> "); // Replace the @ symbol in the file with" "result_des2 = result_des2.replaceAll (" [@] ", ""); br. close (); String filePath = File. getAbsolutePath (); filePath = filePath. replaceAll ("[@]", ""); File file2 = new File (filePath); file. renameTo (file2); FileOutputStream fos = new FileOutputStream (file2); PrintWriter pw = new PrintWriter (new OutputStreamWriter (fos, "gb2312"); pw. write (result_des2); pw. close (); fos. close (); return true;}/*** processing of the close button of the video Layer * @ param frame */public void exit (JFrame frame) {int val = JOptionPan E. showConfirmDialog (frame, "Are you sure you want to leave? "); If (val = JOptionPane. YES_OPTION) {System. exit (0) ;}}// select the File Dialog Box package com. mengdian. findandreplace. ui; import java. awt. borderLayout; import java. awt. flowLayout; import java. awt. event. actionEvent; import java. awt. event. actionListener; import java. awt. event. windowAdapter; import java. awt. event. using wevent; import java. io. file; import javax. swing. JButton; import javax. swing. JFileChooser; import javax. swing. JFrame; import javax. swing. JOptionPane; import javax. swing. JPanel; import javax. swing. JTextField; import com. mengdian. findandreplace. service. findAndReplaceService; public class FindAndReplaceFrame extends JFrame {private String filePath = null; public String getFilePath () {return filePath;} public void setFilePath (String filePath) {this. filePath = filePath;}/*****/private static final long serialVersionUID = 1L; public FindAndReplaceFrame () {init ();} /*** initialization Interface Method */private void init () {// window title this. setTitle ("Searching and replacing files in batches"); // you can specify the size of this. setSize (800,500); // this. setLocationRelativeTo (null); // Add the component this. setContentPane (createContentPanel (); this. setdefaclocloseoperation (JFrame. DO_NOTHING_ON_CLOSE); // click the close button to exit the program. this. addWindowListener (new WindowAdapter () {public void windowClosing (invalid wevent e) {System. exit (0); super. windowClosing (e) ;}});}/*** create the main window * @ return */private JPanel createContentPanel () {JPanel panel = new JPanel (new FlowLayout (100,200, 10); final JTextField jtf = new JTextField ("--------------------------- absolute file path -------------------------"); jtf. setEditable (false); JButton jbt = new JButton ("select file/folder"); panel. add (jtf, BorderLayout. CENTER); panel. add (jbt, BorderLayout. SOUTH); jbt. addActionListener (new ActionListener () {@ Override public void actionreceivmed (ActionEvent e) {JFileChooser jfc = new JFileChooser (); jfc. setFileSelectionMode (JFileChooser. DIRECTORIES_ONLY); int returnVal = jfc. showOpenDialog (jfc); if (returnVal = JFileChooser. APPROVE_OPTION) {filePath = jfc. getSelectedFile (). getAbsolutePath (); jtf. setText (filePath); File file = new File (filePath); try {FindAndReplaceService. replace (file); // The successful replacement dialog box JOptionPane is displayed. showConfirmDialog (FindAndReplaceFrame. this, "replaced successfully", "message", JOptionPane. CLOSED_OPTION); // Click OK to launch} catch (Exception ee) {JOptionPane. showConfirmDialog (FindAndReplaceFrame. this, "failed to replace"); ee. printStackTrace () ;}}}); return panel ;}// main program package com. mengdian. findandreplace. test; import com. mengdian. findandreplace. ui. findAndReplaceFrame; public class Main {public static void main (String [] args) {FindAndReplaceFrame frame = new FindAndReplaceFrame (); frame. setVisible (true );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.