0

I created a code, it works fine and all, but I wanted the program to generate a .txt file with the results to save it for later use. But I want the program to create different files each time I execute it because I plan to use it once a month (it's for a monthly profit calculation) and save it using the year and month numbers to name the file.

For example:

File file = new File("C:\...\YearMonth.txt");

But of course the year and month would depend on the int Year and int Month variables informed previously in the program.

Any ideas? Hope I was clear enough.

3 Answers 3

1

You should create a file meta_data to store info of previous files

So, when you create a new file, you will want to read meta_data file to generate new file name

Hope this could help you

Sign up to request clarification or add additional context in comments.

Comments

1

Is it you want?

    Calendar c = Calendar.getInstance();
    int year       = c.get(Calendar.YEAR);
    int month      = (c.get(Calendar.MONTH) + 1);

    String fileName = "profit_"+ month + "_" + year + ".txt";

    File file = new File("C:...\\" + fileName);

Comments

1

I believe you are using a Stand Alone java Program. When you want to log the results which are printed in Console use below way

    Calendar c = Calendar.getInstance();
    int year       = c.get(Calendar.YEAR);
    int month      = (c.get(Calendar.MONTH) + 1);
    String fileName = "Log_"+year"_"+month+".txt"
   File file = new File (fileName);    
   FileOutputStream  fos = new FileOutputStream(file);
        ps = new PrintStream(fos);  
        System.setOut(ps); // This will write all the console log to a file.
 //when exiting the program or before termination Flush the PrintStream

2 Comments

That's what I thought I should have done but I wasn't sure if that would work! Can I write like this... File file = new File("C:\...\"+fileName); using the code you wrote above?
Yes You can but better configure the File location.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.