Danny
Danny

Reputation: 117

Formatter Errors and Problems

This is not a specific question, but more of a broad one. I am using a Formatter to create a file of results outside of java.

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

For some reason, the things that I am trying to print onto this text file will not appear on DataResults2; however, I can get the things I am trying to print to appear on my console on 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);

As I stated above, the System.out.print will appear on my console however the f.format will not appear on my file.

And yes I do close using f.close();

What are the issues that occur when using formatter? Why would a document not show up however the console print everything correctly?

I understand that you cannot answer my question directly without more code, but I cannot show you my entire code before it is too long. I just want to know some suggestions on what to look for and i will work through my program. Thank you.

Skeet, does this help at all?

 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);
            } 

Upvotes: 0

Views: 123

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109567

At the end you need to call close. Also specifying the character set would be more portable across computers.

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();

If that does not work, then use an absolute path for the file, maybe in the user directory with:

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

No need to do something like createFile.

Upvotes: 1

Related Questions