如何在java中替换文本文件中的行

时间:2016-11-19 12:56:16

标签: java file

我正在阅读一个文本文件,想要替换匹配正则表达式模式的行    {pattern = "^[0-9]+[\\s]*";}

和mycode是:

sc = new Reader();
        out = new PrintWriter (System.out, true);
        PDFTextStripper pdfStripper = null;
        PDDocument pdDoc = null;
        COSDocument cosDoc = null;
        File file ;
        try {

            pdDoc = PDDocument.load(new File("C:\\Users\\Vivek\\Desktop\\novels\\The-End-of-Days-an-Adventure-Love-Story-for-Adults.pdf"));
            pdfStripper = new PDFTextStripper();
            pdfStripper.setStartPage(1);
            pdfStripper.setEndPage(492);


            String parsedText = pdfStripper.getText(pdDoc);

            String pattern = "^[0-9]+[\\s]*";
            parsedText=parsedText.replaceAll(pattern, " ");
            try
            {
                String filename= "C:\\Users\\Vivek\\Desktop\\novels\\Me1.txt";
                FileWriter fw = new FileWriter(filename,true); //the true will append the new data
                fw.write(parsedText);//appends the string to the file
                fw.close();
            }
            catch(IOException ioe)
            {
                System.err.println("IOException: " + ioe.getMessage());
            }


          //  out.println(parsedText);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

    }

我尝试了许多代码,但没有任何作用。

0 个答案:

没有答案