Java Class file

This is a working basic code to create a new file called ‘test.txt’. You can see this code more clearly in my repl under the src folder.

import java.util.Random;
import java.io.*;

public class TestData
{
    //-----------------------------------------------------------------
    //  Creates a file of test data that consists of ten lines each
    //  containing ten integer values in the range 10 to 99.
    //-----------------------------------------------------------------
    public static void main(String[] args) throws IOException
    {
        final int MAX = 10;

        int value;
        String fileName = "test2.txt";

        PrintWriter outFile = new PrintWriter(fileName);

        Random rand = new Random();

        for (int line=1; line <= MAX; line++)
        {
            for (int num=1; num <= MAX; num++)
            {
                value = rand.nextInt(90) + 10;
                outFile.print(value + "   ");
            }
            outFile.println();
        }

        outFile.close();
        System.out.println("Output file has been created: " + fileName);
    }
}

This is a reminder for myself to use for final project for the html editor/compiler I’m working on using JavaFX.

Remember that I have more articles in which I’ve been using Java for mini-programs that I create for my class assignments!. Click here.

Leave a Reply

Back to Top