This program is compiling just fine, and the variables and such seem to be passing through. However, when I open the text file there is no data. Can anyone point me in the right direction or nudge my brain into the right process thought?

package dvdlogger;
import java.util.*;
import java.io.*;

public class DVDLogger {
public DVDLogger() throws IOException
    String title = null;
    double price = 0;
    storeDVDInfo(title, price);
private void storeDVDInfo(String title, double price) throws IOException
    File path = new File("C:\\users\\crazy\\desktop\\dvd.txt");
    PrintWriter output = new PrintWriter(path);
    output.printf("%1$s %2$.2f", title, price); 

public static void main(String[] args) throws IOException{
    DVDLogger dvd;
    String title;
    double price;
    try (Scanner read = new Scanner(System.in)) {
        dvd = new DVDLogger();
        System.out.printf("%nPlease enter the title of the DVD: ");
        title = read.nextLine();
        System.out.printf("%nPlease enter the price of the DVD: ");
        price = read.nextDouble();
    dvd.storeDVDInfo(title, price);

closed as off-topic by rolfl, Adam Dec 9 '13 at 5:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – rolfl, Adam
If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Your question is not suited to CodeReview, it is not working code. Still this is a really common mistake that java programmers make, and the answer is more than can fit in a comment..... will answer anyway.... \$\endgroup\$ – rolfl Dec 9 '13 at 4:20

With any Java Writer or Stream... YOU MUST PROPERLY FLUSH AND CLOSE it.

The documentation is not fantastically clear... but:

when you exit the method, you must ensure that the file is flushed and closed. you are exiting your program before the data is written to disk.

Always use the following pattern when doing IO (if you are using Java7) :

try (PrintWriter output = new PrintWriter(path)) {
    output.printf("%1$s %2$.2f", title, price);

This is a 'try-with-resource' block.

  • \$\begingroup\$ And that nailed it. I had reviewed tons of examples, and don't recall seeing a flush() anywhere. But it now works. Thanks much, and in the future I'll keep posts like these on stackoverflow. \$\endgroup\$ – mbnew Dec 9 '13 at 4:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.