using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;

namespace keylocker
{
    ///
    /// This program creates, print and archives unique 4 digit 
    /// codes, for example for locks. Don't forget to take picture 
    /// from printed code! To be saver. Just add reference Printer in VS.
    /// The CIA security killer:
    /// Raymond,Janick,Lauren Moesker V0.9 8/2016
    /// V1.0 8/2017
    ///
    class KeyLocker
    {
        static void Main(string[] args)
        {
            Console.Clear();
            string path = "keys.txt";
            if (!File.Exists(path))
            {
                try
                {
                    // Create the file.
                    using (FileStream fs = File.Create(path))
                    {
                        Byte[] info = new UTF8Encoding(true).GetBytes("Keys;Date;Time:" + Environment.NewLine);
                        // Add some information to the file.
                        fs.Write(info, 0, info.Length);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }
            String date = DateTime.Now.ToString("yyyy-MM-dd;HH:MM:ss");
            int randomnumber = KeyLocker.GetRandomNumber();
            System.Console.WriteLine("");
            System.Console.WriteLine("########## THE PORTABELLO ESTATE KEYLOCKER ##########");
            System.Console.WriteLine("");
            System.Console.WriteLine("Your Random Number For Today:" + randomnumber);
            string code = randomnumber + ";" + date;
            try
            {
                using (StreamWriter sw = File.AppendText(path))
                {
                    sw.WriteLine(code);
                }
                Console.WriteLine("\nArchived: " + code);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            Console.Write("Do you want to print the key? [Y or N]");
            var print = Console.ReadLine();
            if (print.Equals("N") || print.Equals("n"))
            {
                Console.Write("\nPress any key to continue... ");
                Console.ReadLine();
            }
            else if (print.Equals("Y") || print.Equals("y"))
            {
                try
                {
                    PrintDialog printDialog = new PrintDialog();
                    PrintDocument printDocument = new PrintDocument();
                    printDialog.Document = printDocument;
                    // print three times with line break
                    printDocument.PrintPage += delegate (object sender, PrintPageEventArgs e)
                    {
                        e.Graphics.DrawString(" " + code, new Font("Arial", 18), new SolidBrush(Color.Black), new RectangleF(50, 50, printDocument.DefaultPageSettings.PrintableArea.Width, printDocument.DefaultPageSettings.PrintableArea.Height));
                    };
                    printDocument.PrintPage += delegate (object sender, PrintPageEventArgs e)
                    {
                        e.Graphics.DrawString(" " + code, new Font("Arial", 18), new SolidBrush(Color.Black), new RectangleF(50, 150, printDocument.DefaultPageSettings.PrintableArea.Width, printDocument.DefaultPageSettings.PrintableArea.Height));
                    };
                    printDocument.PrintPage += delegate (object sender, PrintPageEventArgs e)
                    {
                        e.Graphics.DrawString(" " + code, new Font("Arial", 18), new SolidBrush(Color.Black), new RectangleF(50, 250, printDocument.DefaultPageSettings.PrintableArea.Width, printDocument.DefaultPageSettings.PrintableArea.Height));
                    };

                    DialogResult result = printDialog.ShowDialog();

                    if (result == DialogResult.OK)
                    {
                        printDocument.Print();
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
                Console.Write("\nDone Printing.. ");
                Console.ReadLine();
            }
            else
            {
                Console.Write("\nWrong input.\nPress any key to continue... ");
                Console.ReadLine();
            }
        }

        // Return 4 digit random number.
        public static int GetRandomNumber()
        {
            int min = 1000;
            int max = 9999;
            Random rdm = new Random();
            return rdm.Next(min, max);
        }
    }
}
The same program in JAVA:
package keylocker;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.Scanner;

/*
 *   This program creates, print and archives unique 5 digit 
 *   codes, for example for locks. Don't forget to take picture 
 *   from printed code! To be saver. Just add reference Printer in VS.
 *   The CIA security killer:
 *   Raymond,Janick,Lauren Moesker V0.9 8/2016
 *   V1.0 8/2017
 */
public class KeyLocker implements Printable
{
	private static String code = "";

	public static void main(String[] args)
	{
		KeyLocker kl = new KeyLocker();
		kl.run();
	}

	public void run()
	{
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd;HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		String d = dateFormat.format(cal.getTime());
		int randomnumber = GetRandomNumber();
		System.out.println("");
		System.out.println("########## THE PORTABELLO ESTATE KEYLOCKER ##########");
		System.out.println("");
		System.out.println("Your Random Number For Today:" + randomnumber);
		try
		{
			File file = new File("keys.txt");
			if (!file.exists())
			{
				file.createNewFile();
			}
			FileWriter fileWritter = new FileWriter(file.getName(), true);
			BufferedWriter bw = new BufferedWriter(fileWritter);
			code = randomnumber + ";" + d;
			bw.write(code + System.lineSeparator());
			System.out.println("\nArchived: " + code);
			bw.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		System.out.println("Do you want to print the key? [Y or N]");
		@SuppressWarnings("resource")
		Scanner sc = new Scanner(System.in);
		String answer = sc.next();
		if (answer.equals("N") || answer.equals("n"))
		{
			System.out.println("\nPress 2X [ENTER] to close program... ");
			System.console().readLine();
		}
		else 
			if (answer.equals("Y") || answer.equals("y"))
		{
			PrinterJob job = PrinterJob.getPrinterJob();
			boolean ok = job.printDialog();
			if (ok)
			{
				job.setPrintable(this);
				try
				{
					job.print();
				}
				catch (PrinterException ex)
				{
					System.out.println("\nThe print job did not successfully complete... ");
				}
			}
			System.out.println("\nDone Printing.. ");
			System.console().readLine();
		}
		else
		{
			System.out.println("\nWrong input. Press 2X [ENTER] to close program... ");
			System.console().readLine();
		}
		System.exit(0);
	}

	// print three times with line break
	public int print(Graphics g, PageFormat pf, int page) throws PrinterException
	{
		if (page > 0)
		{
			return NO_SUCH_PAGE;
		}
		Font font = new Font("Arial", Font.BOLD, 16);
		Graphics2D g2d = (Graphics2D) g;
		g2d.translate(pf.getImageableX(), pf.getImageableY());
		g2d.setFont(font);
		g.drawString(code, 50, 50);
		g.drawString(code, 50, 150);
		g.drawString(code, 50, 250);
		return PAGE_EXISTS;
	}

	// Return 5 digit random number.
	public static int GetRandomNumber()
	{
		int min = 10000;
		int max = 99999;
		Random rdm = new Random();
		return rdm.nextInt(max) + min;
	}
}