简体   繁体   中英

PrintWriter not writing to .java file

The code compiles and no errors are thrown. However, the text is not printing to my .java file. I tried printing to a .txt file, but that isn't working either. The file I am trying to print to is in the same directory as my Main class. I know that the 2 most common causes of PrintWriter not working are not closing the PrintWriter and not having the correct path. I don't think either one is the issue here, but I may be overlooking something. Any help is much appreciated!

Here is my code with irrelevant bits left out:

public class Main {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() { 
                Gui gui = new Gui();
                gui.createAndShowGUI();
                gui.setVisible(true);
            }
        });

        //System.out.println(new File(".").getAbsolutePath());
        File file = new File("C:\\Users\\Me\\Desktop\\Basket.java");
        PrintWriter pw = null;

        try {
            pw = new PrintWriter(file);
            pw.println("hello there");
            pw.flush();// tried with and without flushing;
            // System.out.println(pw.checkError());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
    }
}

The problem was that I needed to manually refresh my project directory view in Eclipse. Shortcut: F5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM