seasongirl 2016-04-30 09:11 采纳率: 33.3%
浏览 1566

java中从txt文档中提取出来的内容可以在控制台输出,但不能写入到另一个txt文档中

 import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TQ {
public static void main(String[] args) {

        String myRegex1="[0-9.,]+([\\s]?)[%|元|万元|亿元|美元|英镑|千股|万股|千万股|亿股|米|千米|万千米|里|公里|千里|万千里|斤|千克|吨|列|个|项|件|马力|家|辆|次|户|人次|百人次|千人次|万人次]+";//金额
        String myRegex2="([,。;?!])";
        String myRegex3="([0-9.,]+$)+";
        //String myRegex4="[^(元|万元|亿元|美元|英镑|千股|万股|千万股|亿股|米|千米|万千米|里|公里|千里|万千里|斤|千克|吨|列|个|项|件|马力|家|辆|次|户|人次|百人次|千人次|万人次)..]";
        //String myRegex3="[元|万元|亿元|美元|英镑|千股|万股|千万股|亿股|米|千米|万千米|里|公里|千里|万千里|斤|千克|吨|列|个|项|件|马力|家|辆|次|户|人次|百人次|千人次|万人次]+";

        File inFile = new File("G:/Users/liuyang/Desktop/PDF2TXT//中车 【601766.SH】.txt"); // 读取的txt文件
        File outFile = new File("F://TQ.txt");

        String inString = ""; 
        String nextString = ""; 

        java.util.regex.Pattern p1=java.util.regex.Pattern.compile(myRegex1);
        java.util.regex.Pattern p2=java.util.regex.Pattern.compile(myRegex2);
       // java.util.regex.Pattern p3=java.util.regex.Pattern.compile(myRegex3);
        //java.util.regex.Pattern p4=java.util.regex.Pattern.compile(myRegex4);

        try {
               BufferedReader reader = new BufferedReader(new FileReader(inFile));//
           BufferedWriter writer = new BufferedWriter(new FileWriter(outFile));

               while((inString=reader.readLine())!=null){
                   Pattern pattern = Pattern.compile(myRegex3);
                   Matcher matcher = pattern.matcher(inString);

                   if(matcher.find()){

                       nextString=reader.readLine();
                       String ljString=inString+nextString;
                       java.util.regex.Matcher m1=p1.matcher(ljString);
                      while(m1.find()){
                          String matchedText=m1.group();
                          //System.out.println(matchedText);
                          String tmpString=ljString+"$";
                          String zhString=tmpString.substring(tmpString.indexOf(matchedText),tmpString.indexOf("$"));
                          String Text=matchedText.replace(",", "");
                          zhString=zhString.replace(matchedText, Text);

                        java.util.regex.Matcher m11=p2.matcher(zhString);
                        if(m11.find()){
                            zhString=zhString.replace("!", "#").replace(",", "#").replace("。", "#").replace("?", "#").replace(";", "#");
                            String zqString=ljString.substring(0,ljString.indexOf(matchedText));

                            java.util.regex.Matcher m111=p2.matcher(zqString);
                             if(m111.find()){
                                zqString=zqString.replace("!", "@").replace(",", "@").replace("。", "@").replace("?", "@").replace(";", "@");
                                ljString=zqString+zhString;
                                String GJC=ljString.substring(ljString.indexOf("@")+1,ljString.indexOf("#"));
                                GJC=GJC.replace("@", "");
                                System.out.println(GJC+"(ljGJC)");  
                                writer.write(GJC);
                                writer.newLine();
                             }//m111


                        }//m11
                       }//m1

                   }//if(inString以数字结尾)
                   else{
                       java.util.regex.Matcher m1=p1.matcher(inString);
                       while(m1.find()){
                           String matchedText=m1.group();
                           String tmpString=inString+"$";
                           String zhString=tmpString.substring(tmpString.indexOf(matchedText),tmpString.indexOf("$"));
                           String Text=matchedText.replace(",", "");
                           zhString=zhString.replace(matchedText, Text);
                           java.util.regex.Matcher m2=p2.matcher(zhString);
                           if(m2.find()){
                             zhString=zhString.replace("!", "#").replace(",", "#").replace("。", "#").replace("?", "#").replace(";", "#");
                             String zqString=inString.substring(0,inString.indexOf(matchedText));
                             java.util.regex.Matcher m3=p2.matcher(zqString);
                             if(m3.find()){
                                zqString=zqString.replace("!", "@").replace(",", "@").replace("。", "@").replace("?", "@").replace(";", "@");
                                inString=zqString+zhString;
                                String GJC=inString.substring(inString.indexOf("@")+1,inString.indexOf("#"));
                                GJC=GJC.replace("@", "");
                                System.out.println(GJC+"(GJC)");  
                                writer.write(GJC);
                                writer.newLine();
                             }//m3
                             else{
                                zqString="@"+zqString;
                                inString=zqString+zhString;
                                String GJC=inString.substring(inString.indexOf("@")+1,inString.indexOf("#"));
                                GJC=GJC.replace("@", "");
                                System.out.println(GJC+"(GJC)"); 
                                writer.write(GJC);
                                writer.newLine();
                             }
                           }//m2
                           else{
                             nextString=reader.readLine();
                             String ljString=zhString+nextString;
                             java.util.regex.Matcher m21=p1.matcher(ljString);
                             while(m21.find()){
                                String matchedText21=m21.group();
                                String tmpString21=ljString+"$";
                                String zhString21=tmpString21.substring(tmpString21.indexOf(matchedText21),tmpString21.indexOf("$"));
                                String Text21=matchedText21.replace(",", "");
                                zhString21=zhString21.replace(matchedText21, Text21);
                                java.util.regex.Matcher m211=p2.matcher(zhString21);
                                if(m211.find()){
                                    zhString21=zhString21.replace("!", "#").replace(",", "#").replace("。", "#").replace("?", "#").replace(";", "#");
                                    String zqString21=ljString.substring(0,ljString.indexOf(matchedText21));
                                    java.util.regex.Matcher m2111=p2.matcher(zqString21);
                                    if(m2111.find()){
                                        zqString21=zqString21.replace("!", "@").replace(",", "@").replace("。", "@").replace("?", "@").replace(";", "@");
                                        ljString=zqString21+zhString21;
                                        String GJC21=ljString.substring(ljString.indexOf("@")+1,ljString.indexOf("#"));
                                        GJC21=GJC21.replace("@", "");
                                        System.out.println(GJC21+"(GJC21)");
                                        writer.write(GJC21);
                                        writer.newLine();
                                    }
                                    else{
                                        zqString21="@"+zqString21;
                                        ljString=zqString21+zhString21;
                                        String GJC21=ljString.substring(ljString.indexOf("@")+1,ljString.indexOf("#"));
                                        GJC21=GJC21.replace("@", "");
                                        System.out.println(GJC21+"(GJC21)"); 
                                        writer.write(GJC21);
                                        writer.newLine();
                                    }
                                }
                             }
                           }
                       }//m1
                    //System.out.println("字符串sa不是以数字结尾的");
                   }//else(inString不以数字结尾)
               }//while(总)
               reader.close();
               writer.close();
               }
           catch (FileNotFoundException ex) {
                 System.out.println("没找到文件!");
             } catch (IOException ex) {
                 System.out.println("读写文件出错!");
             }

}
}

  • 写回答

1条回答

  • threenewbee 2016-04-30 15:18
    关注

    检查下文件的路径、权限。

    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况