This discussion is archived
4 Replies Latest reply: Sep 22, 2008 7:39 PM by 843785 RSS

filewriter() save to relative directory

843785 Newbie
Currently Being Moderated
Ok ive made a method that saves a file, the only thing is I need it to save to certain location and I dont know how to do that. What I need it to do is when Im running the .jar from the command prompt is to save the file to same directory as .jar file im running. It cant be an exact path because the program has to run off a USB drive which will have a different path depending on the computer. Right now when I run it from inside netbeans it puts the files in my netbeans project folder. When I build the project and run the .jar from the command prompt it puts the files in my vista user folder (c:\users\joseph weaver).

heres a copy of the method
    private static void receipts() throws IOException {
        BufferedWriter output = new BufferedWriter(new FileWriter(lastname +
                " " + semester + ".txt"));

        fileout = ("Name:" + lastname + "," + firstname + "\r\n" +
                "Student ID:" + studentid + "\r\n" + "Semester:" +
                semester + "\r\n" + "Enrolled Classes:" + "\r\n" +
                class1 + "\r\n" + class2 + "\r\n" + class3 + "\r\n" +
                class4 + "\r\n" + "Total Credits:" + credits + "\r\n" 
                +"Total Credit Cost:" + currency.format(tuition) +
                "\r\n" + "Health Insurance:" + currency.format(healthins) 
                + "\r\n" + "Federal Tax:" + currency.format(fedtax) + 
                "\r\n" + "State Tax:" + currency.format(statetax) + 
                "\r\n" + "County Tax:" +currency.format(countytax) + 
                "\r\n" + "Local Tax:" + currency.format(localtax) + 
                "\r\n" + "Total Amount Due:" + currency.format(totaldue));
                
        output.write(fileout);
        System.out.println(fileout);
        output.close();

    }
Edited by: kingweaver88 on Sep 22, 2008 12:33 PM
  • 1. Re: filewriter() save to relative directory
    843785 Newbie
    Currently Being Moderated
    Try this
    System.out.println(new File(YourClassFromJarHere.class.getProtectionDomain().getCodeSource().getLocation().getFile()).getParentFile());
  • 2. Re: filewriter() save to relative directory
    843785 Newbie
    Currently Being Moderated
    so like this?

    im sorry im really a noob, im kinda lost here
    private static void receipts() throws IOException {
            BufferedWriter output = new BufferedWriter(new FileWriter(YourClassFromJarHere.class.getProtectionDomain().getCodeSource().getLocation().getFile()).getParentFile()+(lastname +
                    " " + semester + ".txt")));
     
            fileout = ("Name:" + lastname + "," + firstname + "\r\n" +
                    "Student ID:" + studentid + "\r\n" + "Semester:" +
                    semester + "\r\n" + "Enrolled Classes:" + "\r\n" +
                    class1 + "\r\n" + class2 + "\r\n" + class3 + "\r\n" +
                    class4 + "\r\n" + "Total Credits:" + credits + "\r\n" 
                    +"Total Credit Cost:" + currency.format(tuition) +
                    "\r\n" + "Health Insurance:" + currency.format(healthins) 
                    + "\r\n" + "Federal Tax:" + currency.format(fedtax) + 
                    "\r\n" + "State Tax:" + currency.format(statetax) + 
                    "\r\n" + "County Tax:" +currency.format(countytax) + 
                    "\r\n" + "Local Tax:" + currency.format(localtax) + 
                    "\r\n" + "Total Amount Due:" + currency.format(totaldue));
                    
            output.write(fileout);
            System.out.println(fileout);
            output.close();
     
        }
    Edited by: kingweaver88 on Sep 22, 2008 6:24 PM
  • 3. Re: filewriter() save to relative directory
    843785 Newbie
    Currently Being Moderated
    bump
  • 4. Re: filewriter() save to relative directory
    843785 Newbie
    Currently Being Moderated
    kingweaver88 wrote:
    so like this?
    im sorry im really a noob, im kinda lost here
    You never mention if this compiles, works, whatever. Please ask a better question here before you start with this bump crp.