i am using bluej and this code compiles but doesnt save the input and unique file extension to the same document. I am new so this may seem elementary but any help would be appreciated. thank you
// File with a timestamp in the filename
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;
import java.time.LocalDate;
// Class definition for UniqueFileNameExampleOne
public class SampleMix {
// Main method
public static void main(String[] args) {
try
(Scanner scanner = new Scanner(System.in);
BufferedWriter writer = new BufferedWriter(new FileWriter(generateUniqueFileName())))
{
while (true) {
System.out.print("Press Enter to enter job info or type 'exit'): ");
String answerdate = scanner.nextLine();
if (answerdate.equalsIgnoreCase("exit")) {
break;
}
System.out.print("Job Applied for: ");
String answer1 = scanner.nextLine();
System.out.print("Company: ");
String answer2 = scanner.nextLine();
System.out.print("Platform: ");
String answer3 = scanner.nextLine();
writer.write("Date:" + LocalDate.now());
writer.newLine();
writer.write("Job Applied For: " + answer1);
writer.newLine();
writer.write("Company: " + answer2);
writer.newLine();
writer.write("Platform: " + answer3);
writer.newLine();
writer.write(answerdate);
writer.newLine();
writer.newLine();
}
//} catch (IOException e) {
// System.err.println("Error writing to file: " + e.getMessage());
//try {
// Specify the directory where you want to create the file
String directoryPath = "C:\\Users\\brett\\Documents";
// Generate a unique file name using timestamp
String uniqueFileName = generateUniqueFileName();
// Combine the directory path and unique file name to get the full file path
Path fullPath = Paths.get(directoryPath, uniqueFileName);
// If the directory does not exist, create the directory
if (!Files.exists(fullPath.getParent())) {
Files.createDirectories(fullPath.getParent());
}
// Create the file
Files.createFile(fullPath);
System.out.println("File created successfully: " + fullPath);
}
// Handle file creation errors
catch (IOException e)
{
e.printStackTrace();
}
}
// Helper method to generate a unique file name using a timestamp
private static String generateUniqueFileName()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("-yyyyMMddHHmmss");
String timestamp = dateFormat.format(new Date());
return "file_" + timestamp + ".txt";
}
}