How can I get my code to recognize when another a file has been changed as it is running?

37 Views Asked by At

I have a method in one file, XFunction.java, that rewrites another class in another file, NDrive.java, then calls the rewritten method from the NDrive class. The problem is, when the method is called, my code still seems to think the method is what it was before it was rewritten. In fact, even if ndrive.delete() returns true and ndrive.exists() returns false, my code STILL reads the old method (and the file NDrive.java is still there). Does my approach not make any sense? Is it fundamentally flawed, or can I fix it?

Here's the method below.

    private double evaluate(String exp) {

    File ndrive = new File("NDrive.java");
    PrintWriter write = null;
    Scanner scan = null;

    try {

        scan = new Scanner(ndrive);

    } catch (FileNotFoundException fnfe) {

        System.out.println("File not found");

    }

    int lineCount = 0;
    String contents = "";
    if (scan != null) {
        while (scan.hasNextLine()) {
            String line = scan.nextLine();
            lineCount++;
            if (lineCount == 9) {
                contents += "        double y = " + exp + ";\n";
            } else {
                contents += line + "\n";
            }
        }

        
        try {
            write = new PrintWriter(ndrive);
        } catch (FileNotFoundException fnfe) {
            System.out.println("File not found for writer");
        }

        write.print(contents);
        System.out.print(contents);

        write.close();
        scan.close();


    }

    return NDrive.returnY();
}

The line NDrive.returnY() should return a different output since the body (contents) of that method in Ndrive.java was changed. But it doesn't. Unless the body of returnY() is "changed" to what it already is (is unchanged) which happens after I run the program again without changing the input exp, because the file NDrive.java already contains the new line.

Can anyone help? I've been looking at this for a really, really long time.

0

There are 0 best solutions below