Error happening to do with PrintWriter.println("");

What I am about to show you does have code above it, but I commented it all out other than the class and main method and it still doesn't work - so can't be to do with that?

It is the PrintWriter.println("Hello"); that is the issue. It is println that is underlined red as an error, and any solutions IntelliJ tries to give me do not work.

 String filename = "src/data.csv";
            File file = new File(filename);

            //Creating the csv file if no file is already present.
            if (!file.exists()) ;
            try {
                file.createNewFile();
                System.out.println(file.getName() + "FILE CREATED");
            } catch (IOException e) {
                System.out.println("ERROR CREATING FILE" + e);
            }

            try {
                FileWriter fileWriter = new FileWriter(filename);
                PrintWriter printWriter = new PrintWriter(fileWriter); //then put fileWriter inside printWriter
                PrintWriter.println("Hello");
                printWriter.flush();
                printWriter.close();
                System.out.println("Contents added to file.");
            } catch (IOException e) {
                System.out.println(e);

        }

Has anyone got an idea of what is happening here?



Read more here: https://stackoverflow.com/questions/68463033/error-happening-to-do-with-printwriter-println

Content Attribution

This content was originally published by hellowordly123 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: