曾经遇到了一个问题,需要将项目中所有的JSP都要添加上
<%@ include file=\"/XXXX.jsp\" %>,手动做比较麻烦,于是就自己写了小工具,如下:
1
package
com;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.io.PrintWriter;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 public class dfee {
16
17 static List rtnList = new ArrayList();
18 public dfee() {
19 }
20
21 /**
22 * @param args
23 */
24 public static void main(String[] args) {
25
26 List pathList = new ArrayList();
27 try {
28 pathList = readfile( "" );
29 for ( int i = 0 ; i < pathList.size(); i ++ ) {
30 String tempStr = pathList.get(i).toString();
31 System.out.println(tempStr);
32 addInclude(tempStr);
33 }
34 }
35 catch (IOException e) {
36 System.out.println( " readfile()exception: " + e.getMessage());
37 }
38 }
39
40 /**
41 *删除某个文件夹下的所有文件夹和文件
42 *@paramdelpathstring
43 *@throwsfilenotfoundexception
44 *@throwsioexception
45 *@returnboolean
46 */
47 public static List readfile(String filepath)
48 throws FileNotFoundException, IOException {
49
50 try {
51
52 File file = new File(filepath);
53 if ( ! file.isDirectory()) {
54 rtnList.add(file.getPath());
55 }
56 else if (file.isDirectory()) {
57 String[] filelist = file.list();
58 for ( int i = 0 ; i < filelist.length; i ++ ) {
59 File readfile = new File(filepath + " \\ " + filelist[i]);
60 if ( ! readfile.isDirectory()) {
61 rtnList.add(readfile.getPath());
62 }
63 else if (readfile.isDirectory()) {
64 readfile(filepath + " \\ " + filelist[i]);
65 }
66 }
67
68 }
69
70 }
71 catch (FileNotFoundException e) {
72 System.out.println( " readfile()exception: " + e.getMessage());
73 }
74 return rtnList;
75 }
76
77 public static boolean addInclude(String filePath) {
78
79 List testList = new ArrayList();
80 try {
81
82
83 File f = new File(filePath);
84 InputStreamReader read = new InputStreamReader( new FileInputStream(
85 f), " UTF-8 " );
86 BufferedReader reader = new BufferedReader(read);
87 String line = "" ;
88
89 while ((line = reader.readLine()) != null ) {
90 testList.add(line);
91 System.out.println(line);
92
93 }
94
95 for ( int i = testList.size(); i > 0 ; i -- ) {
96 String tempStr = testList.get(i - 1 ).toString();
97 if (tempStr.trim().equals( " </body> " )) {
98 testList.set(i - 1 ,
99 " <%@ include file=\ " / CSR_Menu.jsp\ " %> " );
100 testList.set(i, " </body> " );
101 testList.add( " </html> " );
102 break ;
103 }
104 }
105
106 final FileWriter writer;
107 final PrintWriter pw;
108 String fileName = filePath;
109 boolean exists = ( new File(fileName)).exists();
110 writer = new FileWriter(fileName, false );
111 pw = new PrintWriter(writer);
112
113 for ( int i = 0 ; i < testList.size(); i ++ ) {
114 pw.println(testList.get(i).toString());
115 System.out.println(testList.get(i).toString());
116 }
117
118 pw.flush();
119 writer.close();
120 }
121 catch (IOException e) {
122 e.printStackTrace();
123 return false ;
124 }
125 return true ;
126 }
127 }
128
129
130
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.InputStreamReader;
11 import java.io.PrintWriter;
12 import java.util.ArrayList;
13 import java.util.List;
14
15 public class dfee {
16
17 static List rtnList = new ArrayList();
18 public dfee() {
19 }
20
21 /**
22 * @param args
23 */
24 public static void main(String[] args) {
25
26 List pathList = new ArrayList();
27 try {
28 pathList = readfile( "" );
29 for ( int i = 0 ; i < pathList.size(); i ++ ) {
30 String tempStr = pathList.get(i).toString();
31 System.out.println(tempStr);
32 addInclude(tempStr);
33 }
34 }
35 catch (IOException e) {
36 System.out.println( " readfile()exception: " + e.getMessage());
37 }
38 }
39
40 /**
41 *删除某个文件夹下的所有文件夹和文件
42 *@paramdelpathstring
43 *@throwsfilenotfoundexception
44 *@throwsioexception
45 *@returnboolean
46 */
47 public static List readfile(String filepath)
48 throws FileNotFoundException, IOException {
49
50 try {
51
52 File file = new File(filepath);
53 if ( ! file.isDirectory()) {
54 rtnList.add(file.getPath());
55 }
56 else if (file.isDirectory()) {
57 String[] filelist = file.list();
58 for ( int i = 0 ; i < filelist.length; i ++ ) {
59 File readfile = new File(filepath + " \\ " + filelist[i]);
60 if ( ! readfile.isDirectory()) {
61 rtnList.add(readfile.getPath());
62 }
63 else if (readfile.isDirectory()) {
64 readfile(filepath + " \\ " + filelist[i]);
65 }
66 }
67
68 }
69
70 }
71 catch (FileNotFoundException e) {
72 System.out.println( " readfile()exception: " + e.getMessage());
73 }
74 return rtnList;
75 }
76
77 public static boolean addInclude(String filePath) {
78
79 List testList = new ArrayList();
80 try {
81
82
83 File f = new File(filePath);
84 InputStreamReader read = new InputStreamReader( new FileInputStream(
85 f), " UTF-8 " );
86 BufferedReader reader = new BufferedReader(read);
87 String line = "" ;
88
89 while ((line = reader.readLine()) != null ) {
90 testList.add(line);
91 System.out.println(line);
92
93 }
94
95 for ( int i = testList.size(); i > 0 ; i -- ) {
96 String tempStr = testList.get(i - 1 ).toString();
97 if (tempStr.trim().equals( " </body> " )) {
98 testList.set(i - 1 ,
99 " <%@ include file=\ " / CSR_Menu.jsp\ " %> " );
100 testList.set(i, " </body> " );
101 testList.add( " </html> " );
102 break ;
103 }
104 }
105
106 final FileWriter writer;
107 final PrintWriter pw;
108 String fileName = filePath;
109 boolean exists = ( new File(fileName)).exists();
110 writer = new FileWriter(fileName, false );
111 pw = new PrintWriter(writer);
112
113 for ( int i = 0 ; i < testList.size(); i ++ ) {
114 pw.println(testList.get(i).toString());
115 System.out.println(testList.get(i).toString());
116 }
117
118 pw.flush();
119 writer.close();
120 }
121 catch (IOException e) {
122 e.printStackTrace();
123 return false ;
124 }
125 return true ;
126 }
127 }
128
129
130