GTU OOP Program - 21

21). Write a program to create a file name 123.txt, if it does not exist. Append a new data to it if it already exist. write 150 integers created randomly into the file using Text I/O. Integers are separated by space.

import java.io.*;

public class Program_21 {
    public static void main(String[] args) {
        File file = new File("program_21.txt");
        if (file.exists()) {
            System.out.println("File already exists");
            System.exit(0);
        }

        try (PrintWriter output = new PrintWriter(file);) {
            // writing 150 integers created randomly to file
            for (int i = 0; i < 150; i++) {
                output.print(((int) (Math.random() * 400) + 1) + " ");
            }
            System.out.println("File program_21.txt created Succesfully!");
        } catch (FileNotFoundException fnfe) {
            System.out.println("Cannot create the file");
            fnfe.printStackTrace();
        }
    }
}
Output
cse gtu, gtu study material for computer engineering students gujarat
technological university oop lab manual solution
cse gtu, gtu study material for computer engineering students gujarat
technological university oop lab manual solution
cse gtu, gtu study material for computer engineering students gujarat
technological university oop lab manual solution

gtu oop lab manual,

oop GTU Practical,

oop Programs,

Object oriented Programming,

Gtu study material 3140705,

OOP 3140705,

oop practical list,

Java Programming,

gtu oop practical,

oop lab solution gtu

Comments

YouTube

Popular posts from this blog

GTU OOP Program - 18

GTU OS Program - 7

GTU OS Program - 3