-1

My program takes in user input, and puts it into a PrintWriter.

I am unsure of how to change the directory that the PrintWriter saves the text file to. I also need the name of the files to dynamically change based on user input. Here is the code for the PrintWriter:

PrintWriter writer = new PrintWriter(
        "ChangeLog" + textField.getText() + textField_1.getText() + textField_9.getText() + ".txt",
        "UTF-8");
writer.println("Version Number: " + version);
writer.println("Start Date: " + textField.getText());
writer.println("Start time: " + textField_1.getText());
if (rdbtnYes.isSelected()) {
    writer.println("Change was documented in the IT info sheet.");
}
if (rdbtnNo.isSelected()) {
    writer.println("Change was NOT documented in the IT info sheet.");
}
writer.println("Budget Implecation(S): " + textField_2.getText());
writer.println("Server/Network Device: " + textField_3.getText());
writer.println("Process Of Changes Made: " + textField_4.getText());
writer.println("Need(s)/Reason(s) for Change: " + textField_5.getText());
writer.println("Issues/Problems: " + textField_6.getText());
writer.println("Outcome/Results: " + textField_7.getText());
writer.println("Notes/Comments/Other Info" + textField_8.getText());
writer.close();
  • Take a look [here](https://stackoverflow.com/questions/11496700/how-to-use-printwriter-and-file-classes-in-java) – Dwhitz Jun 09 '17 at 15:03

2 Answers2

2

The constructor you are using takes a String argument - denoting a file name.

File names can be just that; or they can include path information. You want a different path - then change the filename to include that path!

See here or this for relative vs. absolute paths.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

One way to do this is that you will need to create a FileWriter object and tell it what location will the file sit where you want to write stuff. Then you'll pass your FileWriter object as an argument to the PrintWriter constructor. See the example below:

FileWriter  writer      = new FileWriter("d:\\path_to_directory\\report.txt");
PrintWriter printWriter = new PrintWriter (writer);

Hope this helps! :)

Eric Fernandes
  • 106
  • 1
  • 6