0

这不是一个具体的问题,而是一个更广泛的问题。我正在使用格式化程序在 java 之外创建结果文件。

Formatter f = new Formatter(new File("DataResults2")); 

出于某种原因,我试图打印到这个文本文件上的东西不会出现在 DataResults2 上;但是,我可以让我试图打印的东西出现在 Eclipse 的控制台上。

System.out.printf("Market Id %d, Contracts %s, Data Points %d, Starting Date %d,
Ending Date %d\n", firstVariable, set.size(), dataPointCounter, firstDateHolder,    
lastDateHolder);
f.format("%d, %s, %d, %d, %d, ", firstVariable, set.size(),
                    dataPointCounter, firstDateHolder, lastDateHolder);

如上所述,System.out.print将出现在我的控制台上,但f.format不会出现在我的文件中。

是的,我确实使用f.close();

使用格式化程序时会出现什么问题?为什么文档不会显示,但控制台会正确打印所有内容?

我知道如果没有更多代码,您无法直接回答我的问题,但我无法在太长之前向您展示我的整个代码。我只是想知道一些关于寻找什么的建议,我将完成我的程序。谢谢你。

斯基特,这有帮助吗?

 Iterator itr = set.iterator();
        while (itr.hasNext())
        { //while
            int contractIDDisplay = (int) itr.next();
            if (contractIDDisplay == 1)
            {
                System.out.printf("%d, %d, %d, %d",contractIDDisplay, monthCounter1, firstDate1, lastDate1);
                f.format("%d, %d, %d, %d, ",contractIDDisplay, monthCounter1, firstDate1, lastDate1);
            } 
4

1 回答 1

1

最后你需要调用close。此外,指定字符集将更便于跨计算机移植。

Formatter f = new Formatter(new File("DataResults2"), "Windows-1252",
                            Locale.US); // Latin-1 on Windows, US, Part of Europe
Formatter f = new Formatter(new File("DataResults2"), "UTF-8",
                            Locale._US); // International Unicode, US number notation

f.close();

如果这不起作用,则使用文件的绝对路径,可能在用户目录中:

String path = System.getProperty("user.home") + File.separator + "DataResults2.txt";

无需执行 createFile 之类的操作。

于 2013-06-17T13:09:06.083 回答