0% found this document useful (0 votes)
43 views1 page

OOPJ1

This program creates a file called 123.txt, appends new data to it if it already exists, and writes 150 randomly generated integers separated by spaces into the file using text I/O. It uses a PrintWriter to write to a FileOutputStream with the file 123.txt in append mode, and generates random integers between 0-150 in a for loop, printing each one with a space to the file. Any errors are caught and printed.

Uploaded by

krunalam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views1 page

OOPJ1

This program creates a file called 123.txt, appends new data to it if it already exists, and writes 150 randomly generated integers separated by spaces into the file using text I/O. It uses a PrintWriter to write to a FileOutputStream with the file 123.txt in append mode, and generates random integers between 0-150 in a for loop, printing each one with a space to the file. Any errors are caught and printed.

Uploaded by

krunalam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

No. 20 : 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.

Code:

import java.io.*; import


java.util.Scanner; class
Practical_21
{
public static void
main(String[] args)
{
try
(
PrintWriter pw = new PrintWriter(new FileOutputStream(new File("123.txt"), true));
){
for (int i = 0; i < 150; i++)
{
pw.print((int)(Math.random() * 150) + " ");
}
}
catch (FileNotFoundException fnfe)
{
System.out.println("Cannot create the file.");
fnfe.printStackTrace();
}
}
}

Output:It is create a txt file which name is 123.txt

32

You might also like