Java编译错误:忽略新实例

时间:2014-03-13 09:00:30

标签: java

这是我使用itext创建pdf文档的java代码。

package com.cdac.pdfparser;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;

public class PDFCreate {
    public static String RESULT = "results/part1/chapter01/";
    public static void main(String[] args)
    throws DocumentException, IOException {
        Scanner sc = new Scanner(System.in);
        String fileName = sc.nextLine();
        RESULT = RESULT + fileName;
        new PDFCreate.createPdf(RESULT);
    }
    public void createPdf(String filename)
    throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        // step 5
        document.close();
    }
}

但是我收到了编译错误:忽略了新实例

请帮帮我......

2 个答案:

答案 0 :(得分:4)

    new PDFCreate.createPdf(RESULT);
          -------^

这不是创建Object的正确方法。

应该是

 new PDFCreate().createPdf(RESULT);

你忘了写()

答案 1 :(得分:0)

我使用新的PDFCreate()更改新的PDFCreate.createPdf(RESULT)。createPdf(RESULT);

新的 PDFCreate.createPdf(RESULT)是在java中创建对象的正确方法。

希望它有效

 package com.cdac.pdfparser;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;




  public class PDFCreate {
    public static String RESULT = "results/part1/chapter01/";
    public static void main(String[] args)
    throws DocumentException, IOException {
     Scanner sc = new Scanner(System.in);
     String fileName = sc.nextLine();
     RESULT = RESULT + fileName;
     new PDFCreate().createPdf(RESULT);
    }
     public void createPdf(String filename)
    throws DocumentException, IOException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        document.add(new Paragraph("Hello World!"));
        // step 5
       document.close();

} }