Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用file return by 1方法将文件返回到同一类的另一个方法中_Java_File Io_Io - Fatal编程技术网

Java 如何使用file return by 1方法将文件返回到同一类的另一个方法中

Java 如何使用file return by 1方法将文件返回到同一类的另一个方法中,java,file-io,io,Java,File Io,Io,步骤1:用户单击按钮文件即可下载 步骤2:该pdf的内容已被读取 应为:下载前,如果文件已存在,请删除该文件 已尝试: public File A() { String userDir = System.getProperty("user.home")+"\\Downloads"; File file = new File(userDir+"\\PDFStatement.pdf"); return file; } publ

步骤1:用户单击按钮文件即可下载

步骤2:该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中拥有所有内容(除了删除内容)(如声明和创建文件),则在下载之前可以正常工作

,如果文件已经存在,则删除文件
-其中;在服务器上还是在客户端机器上?在测试下载文件的本地机器上