Emre Kevin Pakoy
Emre Kevin Pakoy

Reputation: 3

Generating Code into an existing file with Java

I am trying to write a code that will generate code into an already existing HTML File. It seems like I can not reach the existing HTML file in my repository.

I would be happy if someone could help.

Here is the method that should do the code generation:

public static void generate() {
        PrintWriter pWriter = null;
        try {
            pWriter = new PrintWriter(new BufferedWriter(new FileWriter("<filename>.html"))); //and path
            pWriter.println("<code we want to put in>");
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (pWriter != null){
                pWriter.flush();
                pWriter.close();
            }
        }
    }

Upvotes: 0

Views: 57

Answers (1)

devNNP
devNNP

Reputation: 59

  1. Check your file read and write access. If you use Mac-OS or linux try to execute chmod 666 .html
  2. If you use Java SE 7+, you can use try-with-resources with PrintWriter.
  3. Check the path to your file.

Try this code below:

public static void generate() {
    try (PrintWriter pWriter = new PrintWriter(new File("test.html"))){
        pWriter.println("<CODE>");
        pWriter.flush();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

Upvotes: 1

Related Questions