Question: Could you please help me on this JAVA program 17.1 (Create a text file) Write a program to create a file named Exercise17_01.txt if it

Could you please help me on this JAVA program

17.1

(Create a text file) Write a program to create a file named Exercise17_01.txt if it does not exist. Append new data to it if it already exists. Write 100 integers created randomly into the file using text I/O. Integers are separated by a space.

17.5 (Store objects and arrays in a file) Write a program that stores an array of five int values 1,2,3,4 and 5, a Date object for the current time, and the double value 5.5 into the file named Exercise 17_05.dat. in the name program, write code to read and display the data.

I tried but doesn't work, please help me

import java.io.*;

import java.util.*;

public class Exercise_17_01 {

public static void main(String[] args) throws FileNotFoundException {

// Create an array list of string

ArrayList list = new ArrayList<>();

// Check if file exist

File file = new File("Exercise17_01.txt");

if (file.exists()) {

try (

// Create input file

Scanner input = new Scanner(file);

) {// Read data from file

while (input.hasNext()) {

list.add(input.nextLine());

}

}

}

// Generate 100 integers randomly

for (int i = 0; i < 100; i++) {

list.add(((int)(Math.random() * 100)) + " ");

}

try (

// Create output file

PrintWriter output = new PrintWriter(file);

) {

// write to file using text I/O

for (String l: list) {

output.print(l);

}

}

}

}

/*********************************************************************************

* (Store objects and arrays in a file) Write a program that stores an array of *

* the five int values 1, 2, 3, 4, and 5, a Date object for the current time, and *

* the double value 5.5 into the file named Exercise17_05.dat. *

*********************************************************************************/

import java.io.*;

public class Exercise17_05 {

public static void main(String[] args) throws IOException {

// Create an array of integers

int[] numbers = {1, 2, 3, 4, 5};

try ( // Create an output stream for file Exercise17_05.dat

ObjectOutputStream output = new ObjectOutputStream(new

FileOutputStream("Exercise17_05.dat"));

) {

// Write an array, double value, and Date object to the file

output.writeObject(numbers);

output.writeDouble(5.5);

output.writeObject(new java.util.Date());

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!