Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: trying to merge two sets of coding to get input written to file and auto generate unique saved file

  1. #1
    Junior Member
    Join Date
    Dec 2024
    Location
    california
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default trying to merge two sets of coding to get input written to file and auto generate unique saved file

    trying to merge two sets of coding to get input written to file and auto generate unique saved file. the file compiles and works but it creates two files and does not save the input to the unique file created. i think the problem is in the code line: "BufferedWriter writer = new BufferedWriter(new FileWriter(generateUniqueFileName()))) " but i am new so idk. thnx

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

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trying to merge two sets of coding to get input written to file and auto generate unique saved file

    How are you trying to debug the program to see what it is doing? Try add some print statements that show what values the variables have as they are used.

    Also please copy the full text of any error messages and paste it here.

    Please explain what the code is supposed to do and compare that to what it actually does.
    The program writes the received input to one file and creates another file.
    What did you expect it to do?
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Bdill7 (December 18th, 2024)

  4. #3
    Junior Member
    Join Date
    Dec 2024
    Location
    california
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Talking Re: trying to merge two sets of coding to get input written to file and auto generate unique saved file

    i am using bluej and it compiles and runs and i dont get any error messages. originally where this statement is: BufferedWriter writer = new BufferedWriter(new FileWriter(generateUniqueFileName()))) i had BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\bdill\\Documents\\jobapps.tx t"))) and it would create two different files...one with the information i had input and one with a unique file name.

    and i am trying to get the code to write a file with input that answers the questions in the code and to save that with a unique file name. thnx

    thank you for your help

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trying to merge two sets of coding to get input written to file and auto generate unique saved file

    i am trying to get the code to write a file with input that answers the question and to save that with a unique file name.
    Yes, the program does that.

    It also creates a file that is not used, with this statement:
    Files.createFile(fullPath);
    What do you want that is different from the above?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Junior Member
    Join Date
    Dec 2024
    Location
    california
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: trying to merge two sets of coding to get input written to file and auto generate unique saved file

    I just want the input to be in the program when i save it. i have made some other programs that do that and work good...the only difference is that i wanted a unique name because sometimes i open the program more than once a day, the original program saves the file with the date automatically, so if i open it again it will overwrite the original and i will loose my information. i am trying to write another that will add a unique file extension so that it will be different than any previous i made that day. here is what normally is saved with the other program (with my new program i listed the contents are empty):

    Date:2024-12-17
    Job Applied For: Freight Forwarder/3PL Logistics Operations & Customer Service'
    Company: Mascot International Logistics Inc
    Platform: Indeed.com


    Date:2024-12-17
    Job Applied For: Service Advisor
    Company: BMW of Riverside
    Platform: Indeed.com


    Date:2024-12-17
    Job Applied For: Dynamic PC Support
    Company: Worldwide TechServices
    Platform: Indeed.com

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trying to merge two sets of coding to get input written to file and auto generate unique saved file

    The program creates two files with unique names. It writes data to the first file that is created. The second file is left empty.
    What do you want it to do?

    One approach to prevent overwriting an existing file, is to test if the proposed output file exists. If it does increment a counter to add to the name and test again. Continue until no existing file is found. The created files could be named like this:
    output.txt
    output1.txt
    output2.txt
    output3.txt
    etc
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Bdill7 (December 19th, 2024)

  9. #7
    Junior Member
    Join Date
    Dec 2024
    Location
    california
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: trying to merge two sets of coding to get input written to file and auto generate unique saved file

    Thank you for your response and info...
    you asked: What do you want it to do?
    I want it to write the input to the same file it saves...i felt like i was close but couldnt figure out why it was making two files. if i take out the input it creates a file with a unique ext with the date...i just want it to do that and write the input to that file only and not create any additional files.

    I will try you suggestion to have it check for existing files with the same name...thank you again!

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trying to merge two sets of coding to get input written to file and auto generate unique saved file

    I think that solution will guarantee that the program will not overwrite an existing file.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Bdill7 (December 19th, 2024)

Similar Threads

  1. File Input- Multiple Sets of Data
    By byteit in forum Java Theory & Questions
    Replies: 1
    Last Post: May 6th, 2014, 04:37 AM
  2. Replies: 6
    Last Post: March 24th, 2014, 06:19 AM
  3. generate unique 9 digit number bug
    By matinm90 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 22nd, 2012, 03:21 PM
  4. Help me out with code for uploading saved file in perticular format
    By sweet_guy5914 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 18th, 2012, 06:30 PM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM

Tags for this Thread