Hi,

I have a program that uses JButtons (under ActionEvent e) and the user clicks a button and it brings up the appropriate text file associated with that name. I have the files on a flash drive but every time i transfer the files to different computer and want to run the program on the computer, i need to change the file pathway. They are all loaded into my source package. Is there a way or code that I can have Java go into the source package to retrieve the files or to search for the files and set that pathway as a temporary string? If needed, i'll provide more code.

Thanks for your help!

JButton temp = (JButton) e.getSource();
File f = new File("J:\\AP Comp Sci\\Bible 2\\src\\bible2\\Books\\"
                + temp.getText() + ".txt");
        
        Desktop desktop = Desktop.getDesktop();
        try {
            desktop.open(f);
        } catch (Exception e1) {
            System.out.println("error");
    }
Member Avatar for ztini
File f = new File(getClass().getResource("bible2/Books/" + emp.getText() + ".txt");

This would essentially be C:/...../src/bible2/Books/string.txt

Hope that helps!

It is showing me an error with the second word "File". It says "cannot find symbol" symbol: constructor File(java.net.URL) Location java.io.File.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.