情景是这样的,我家大宝贝备考cpa,有很多word文档,她想把很多文档中的例题全部提取出来,然后生成另一个文档。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class MyBigHoney {
public static void main(String[] args) throws FileNotFoundException {
for (int x=1;x<=12;x++){
String text = "";
File file = new File("C:/BigHoney/00"+x+".doc");
//2003
if(file.getName().endsWith(".doc")){
try {
FileInputStream stream = new FileInputStream(file);
WordExtractor word = new WordExtractor(stream);
text = word.getText();
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// System.out.println(text);
char[] liti= text.toCharArray();//存放例题的数组
char[] daan= text.toCharArray();//存放例题的数组
int[] daanbre= new int [100000];
int[] daanaft= new int [100000];//答案的后标
int[] litibre= new int [100000];//例题的前标
int[] litiaft= new int [100000];
int bb = 0;
int af = 0;
int cc=0;
int dd=0;
for(int i=0;i<daan.length;i++){
if(daan[i]=='【'&&daan[i+1]=='答'&&daan[i+2]=='案'){
daanbre[cc]=i;
cc=cc+1;
for(int j=i;j<i+10;j++){
if(liti[j]=='】'){
daanaft[dd] = j;
dd=dd+1;
break;
}
}
}
}
///
for(int i=0;i<liti.length;i++){
if(liti[i]=='【'&&liti[i+1]=='例'&&liti[i+2]=='题'){
litibre[bb]=i;
bb=bb+1;
for(int j=i;j<i+10;j++){
if(liti[j]=='】'){
litiaft[af] = j;
af=af+1;
break;
}
}
}
}
String result = "";
FileOutputStream out = new FileOutputStream("C:/BigHoney/第00"+x+"章例题.doc");
PrintStream ps = new PrintStream(out);
for(int a=0;a<=litibre.length;a++){
if(litibre[a]!=0&&daanaft[a]!=0)
ps.print( text.substring(litibre[a], daanaft[a]+4));
// System.out.println(text.substring(litibre[a], daanaft[a]+4));
else
break;
}
System.out.println(result);
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class MyBigHoney {
public static void main(String[] args) throws FileNotFoundException {
for (int x=1;x<=12;x++){
String text = "";
File file = new File("C:/BigHoney/00"+x+".doc");
//2003
if(file.getName().endsWith(".doc")){
try {
FileInputStream stream = new FileInputStream(file);
WordExtractor word = new WordExtractor(stream);
text = word.getText();
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
// System.out.println(text);
char[] liti= text.toCharArray();//存放例题的数组
char[] daan= text.toCharArray();//存放例题的数组
int[] daanbre= new int [100000];
int[] daanaft= new int [100000];//答案的后标
int[] litibre= new int [100000];//例题的前标
int[] litiaft= new int [100000];
int bb = 0;
int af = 0;
int cc=0;
int dd=0;
for(int i=0;i<daan.length;i++){
if(daan[i]=='【'&&daan[i+1]=='答'&&daan[i+2]=='案'){
daanbre[cc]=i;
cc=cc+1;
for(int j=i;j<i+10;j++){
if(liti[j]=='】'){
daanaft[dd] = j;
dd=dd+1;
break;
}
}
}
}
///
for(int i=0;i<liti.length;i++){
if(liti[i]=='【'&&liti[i+1]=='例'&&liti[i+2]=='题'){
litibre[bb]=i;
bb=bb+1;
for(int j=i;j<i+10;j++){
if(liti[j]=='】'){
litiaft[af] = j;
af=af+1;
break;
}
}
}
}
String result = "";
FileOutputStream out = new FileOutputStream("C:/BigHoney/第00"+x+"章例题.doc");
PrintStream ps = new PrintStream(out);
for(int a=0;a<=litibre.length;a++){
if(litibre[a]!=0&&daanaft[a]!=0)
ps.print( text.substring(litibre[a], daanaft[a]+4));
// System.out.println(text.substring(litibre[a], daanaft[a]+4));
else
break;
}
System.out.println(result);
}
}
}