首页 > 解决方案 > 如何使用一种方法的文件返回到同一类的另一个方法中

问题描述

Step1:用户点击按钮文件被下载。

Step2:读取该pdf的内容。

预期:在下载之前删除文件,如果它已经存在

试过:

    public File A() {
        String userDir = System.getProperty("user.home")+"\\Downloads";
        File file = new File(userDir+"\\PDFStatement.pdf");
        return file;
    }

    public void clickxxxButton() {
        try {
            File f=pst.A();
            if (f.exists()) // to delete the already existing file before downloading
            {
                f.delete();
            }
        }
        catch (Exception e){
            e.getMessage();
        }
        clickOn(xxxxButton);// here pdf file gets downloaded
    }


    public void verifyPDFContents() {
        try {

            //Loading an existing document

            File file =pst.A();// here pst is the object of the same class where this method resides

            PDDocument document = PDDocument.load(file);
              //Instantiate PDFTextStripper class
              PDFTextStripper pdfStripper = new PDFTextStripper();

              //Retrieving text from PDF document
              String text = pdfStripper.getText(document);
              System.out.println(text);

              //Closing the document
              document.close();
              file.delete() 
        }
         catch (IOException e) {            
            e.printStackTrace();
        }
    }   

我收到 pageobject 依赖注入错误。虽然如果我在 verifypdfcontent 中拥有所有内容(除了 delete )(比如声明和创建文件),它工作正常

标签: javafile-ioio

解决方案


推荐阅读