JDK 7:现有文件在新文件上变空("path/to/file.html");

JDK 7: Existing file gets empty on new File("path/to/file.html");

我正在使用 JDK 7. 我有一个 class 方法,它使用 PrintStream 创建 html 文件。同一个 class 中的另一个方法应该使用创建的文件并对其进行处理。问题是,一旦我使用 new File("path/to/file.html),文件长度就会减少到 0。我的代码:

public class CreatePDFServiceImpl {

private final PrintStream printStream;

public CreatePDFServiceImpl() throws IOException {
    printStream = new PrintStream("/mnt/test.html", "UTF-8");
}

public void createHtmlFile() throws IncorporationException {
    try {
        StringBuilder html = new StringBuilder();
        HtmlFragments htmlFragments = new HtmlFragments();
        html.append(htmlFragments.getHtmlTop())
 .append(htmlFragments.getHeading())
 .append(htmlFragments.buildBody())
 .append(htmlFragments.buildFooter());
        printStream.print(html.toString());
    }  finally {
        if(printStream != null) {
            printStream.close();
        }
    }
}

下一个方法应该使用在 "createHtmlFile()" 中创建的 html 文件:

 public void convertHtmlToPdf() {
    PrintStream out = null;
    try {
        File file = new File("/mnt/test.html");
        /** this code added just for debug **/
        if (file.createNewFile()){
            System.out.println("File is created!");
        } else {
            System.out.println("File already exists. size: " + file.length());
        }

        /* PDF generation commented out. */
        //out = new PrintStream("/mnt/testfs.pdf", "UTF-8");
        //defaultFileWriter.writeFile(file, out, iTextRenderer);

    } catch (IOException e) {
        throw new IncorporationException("Could not save pdf file", e);
    } finally {
        if(out != null) {
            out.close();
        }
    }

我的junit集成测试class:

@Category(IntegrationTest.class)
public class CreatePDFServiceIntegrationTest {

private static CreatePDFServiceImpl createPDFService;

@BeforeClass
public static void init() throws IOException {
    createPDFService = new CreatePDFServiceImpl();
}

@Test
public void testCreateHtmlFile() throws IncorporationException {
    createPDFService.createHtmlFile();
    File createdFile = new File("/mnt/test.html");
    System.out.println("createdFile.length() = " + createdFile.length());
    Assert.assertTrue(createdFile.length() > 1);
}

@Test
public void testCreatePDF() throws Exception {
    File fileThatShouldExist = new File("/mnt/testfs.pdf");
    createPDFService.convertHtml2Pdf();

    Assert.assertTrue(fileThatShouldExist.exists());
}
}

第一次测试通过,输出:

 "createdFile.length() = 3440".

我检查了文件系统,有文件。大小 3,44kb.

第二次测试失败,CreatePDFServiceImpl 的输出:

"File already exists. size: 0"

查看文件系统,现在的文件实际上是0字节。

我很难过。新文件("path")应该只创建对该文件的引用而不是清空它?

我怀疑 File.createNewFile() 中有错误。我还没有完全掌握您 运行 您的代码的顺序,但是您知道这会将文件大小设置为零吗?

out = new PrintStream("/mnt/testfs.pdf", "UTF-8");

来自 PrintStream(File file) Javadoc:

file - The file to use as the destination of this print stream. If the file exists, then it will be truncated to zero size; otherwise, a new file will be created. The output will be written to the file and is buffered.

我认为这是罪魁祸首 - 但在您的代码中,该行被注释掉了。我说得对吗?运行 你的测试中注释了那行?