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";
    }

}

Would you please be able to give a little bit more background into what this code is supposed to do, and exactly what fails? For example, you wrote it doesn't save the input and file extension to the same document. What does that mean? On line 72 of your code, does it successfully create a directory when it needs to? On line 76, does it create a file, but just not the correct file? What is in the file? I'm not seeing anywhere in your code where you write anything to the file. Is the intent just to create an empty file? Is the code being run from a user account that has permission to access C:\Users\brett\Documents?

commented: thanks +0

i would like it to create a file and write input to that file (questions regarding employment applications in this instance) then add a file extension with the date and a unique number so that if i open the file a second time in one day it does not overwrite the earlier file that has the same date...thanks

so for example...i have already created one (well i combined some programs to make it work) that will ask these questons: (it will auto add the date for each question series see below)
Job Applied for: Programmer
Company: Daniweb
Platform: Indeed
Press Enter to list job or type 'exit'to quit:
Job Applied for: Java Programmer
Company: Daniweb
Platform: Monster
Press Enter to list job or type 'exit'to quit: exit

the files saves with the date:
Job_Apps.txt2024-12-19

when i open it it looks like this:
Date:2024-12-19
Job Applied For: Programmer
Company: Daniweb
Platform: Indeed

Date:2024-12-19
Job Applied For: Java Programmer
Company: Daniweb
Platform: Monster

so what i am trying to do is add a unique identifier to the file extension
Job_Apps.txt2024-12-19 + unique identifier
so that if i open it twice in one day it does not overwrite the original or any other files...thanks

This will add a uniqie identifer to a new file each time you run it. It creates a new .txt file each time.
Be sure you have permissions, e.g., run VS Code as Administrator, and the directory exists.

import java.io.BufferedWriter;
import java.io.FileWriter;
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.util.Scanner;
import java.util.UUID;
import java.time.LocalDate;

public class SampleMix {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        BufferedWriter writer = null;

        try {
            // Generate a unique file name using timestamp
            String uniqueFileName = generateUniqueFileName();

            // Specify the directory where you want to create the file
            String directoryPath = System.getProperty("user.home") + "\\Documents\\nasa";

            // 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);

            // Initialize the writer with the full file path
            writer = new BufferedWriter(new FileWriter(fullPath.toString()));

            System.out.println("File created successfully: " + fullPath);

            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.newLine();
            }
        } catch (IOException e) {
            System.err.println("Error writing to file: " + e.getMessage());
        } finally {
            try {
                if (writer != null) {
                    writer.close();
                }
                scanner.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // Helper method to generate a unique file name using a timestamp and UUID
    private static String generateUniqueFileName() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = dateFormat.format(new Date());
        String uniqueID = UUID.randomUUID().toString();
        return "Job_Apps_" + date + "_" + uniqueID + ".txt";
    }
}

PS C:\Users\nasa\Documents\brett> java SampleMix.java
File created successfully: C:\Users\nasa\Documents\nasa\Job_Apps_20241224_250d5919-80ff-40a8-aa81-a94d33488824.txt
Press Enter to enter job info or type 'exit':
Job Applied for: Programmer
Company: DaniWeb
Platform: Indeed
Press Enter to enter job info or type 'exit':
Job Applied for: Java Programmer
Company: DaniWeb
Platform: Monster
Press Enter to enter job info or type 'exit': exit
PS C:\Users\nasa\Documents\brett> java SampleMix.java
File created successfully: C:\Users\nasa\Documents\nasa\Job_Apps_20241224_1c6ccab4-92e4-4384-b247-cb1467e79d68.txt
Press Enter to enter job info or type 'exit':
Job Applied for: Software Engineer
Company: DaniWeb
Platform: USAjobs
Press Enter to enter job info or type 'exit': exit

Job_Apps_20241224_250d5919-80ff-40a8-aa81-a94d33488824.txt
Date: 2024-12-24
Job Applied For: Programmer
Company: DaniWeb
Platform: Indeed

Date: 2024-12-24
Job Applied For: Java Programmer
Company: DaniWeb
Platform: Monster

Job_Apps_20241224_1c6ccab4-92e4-4384-b247-cb1467e79d68.txt
Date: 2024-12-24
Job Applied For: Software Engineer
Company: DaniWeb
Platform: USAjobs

i actually don't understand what it is.

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.