(Create a text file) Write a
Create a text file
Program Plan:
- Declare the class “writeText”.
- Definition for main class.
- Create an object to “PrintWriter” to create a new file named “Exercise17_01.txt”.
- For loop to iterate till 100.
- Write the 100 randomly generated integers to the file.
Program to create a file named “Exercise17_01.txt” to write 100 integers randomly to the created text file.
Explanation of Solution
Program:
//Import required packages
import java.io.*;
import java.util.*;
//Definition for class "writeText"
public class WriteText {
// Definition for main class
public static void main(String[] args) throws IOException {
// Try block
try (
/* Create an object for printWriter to write output to the text file */
PrintWriter out = new PrintWriter(new FileOutputStream(
"Exercise17_01.txt", true));)
{
// Loop to iterate
for (int j = 0; j < 100; j++)
// Generate the random number
out.print((int) (Math.random() * 100000) + " ");
}
}
}
Screenshot of the file “Exercise17_01.txt”
Want to see more full solutions like this?
Chapter 17 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Additional Engineering Textbook Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
C++ How to Program (10th Edition)
Artificial Intelligence: A Modern Approach
Starting Out with Java: From Control Structures through Data Structures (3rd Edition)
Java How To Program (Early Objects)
- Write code that does the following: opens an output file with the filename number_list.txt, uses a loop to write the numbers 1 through 100 to the file, and then closes the file.arrow_forward(Use DevC++) 2. Student GradeWrite a program that reads a student name followed by five test scores. The program should output the student name, the five test scores, and the average test score. Output the average test score withtwo decimal places. The data to be read is stored in a file called test.txt. The output should be stored in a file calledtestavg.out. Input: A file containing the student name and the five test scores. A sample input is:Andrew Miller 87.50 89 65.75 37 98.50 Output: The student name, the five test scores, and the average of the five test scores, saved to a file. The program needs to read a student’s first and last name and five test scores. Therefore, you need two variables to store the student name and five variables to store the five test scores. To find theaverage, you must add the five test scores and then divide the sum by 5. Thus, you need a variable to store the average test score. Furthermore, because the input data is in a file, you need an…arrow_forward*Code in Python Write a script named dif.py. This script should prompt the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the script should simply output "Yes". If they are not, the script should output "No", followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file, including whitespace and punctuation. The loop should break as soon as a pair of different lines is found. Below are examples of the program input and output along with relevant files: one.txt: This is text. abc 123 sameAsOne.txt: This is text. abc 123 differentFromOne.txt This is text. xyz 123 Comparing files that are the same: Enter the first file name: one.txt Enter the second file name: sameAsOne.txt Yes Comparing files that are different: Enter the first file name: one.txt Enter the second file name: differentFromOne.txt No abc xyzarrow_forward
- Make the following modifications for the solution above: name the output/input file "myRandomNumbers.txt" use a loop to output 17 random numbers to the file after creating the file, close it open the file for input and read the contents and then total the numbers in the file determine the average of the numbers in the file determine the highest and lowest numbers in the file DO NOT USE PYTHON BUILTIN FUNCTIONS TO CALCULATE THE AVERAGE OR THE HIGHEST AND LOWEST NUMBERS an expected number of modules/functions would be in the 5-8 range Display your results: the numbers generated on a single horizontal line the calculated total and average the highest and lowest number in the file Be sure to use clear prompts/labeling for input and output.arrow_forwardPlease use C++ code and only use the libraries <iostream> and <fstream>. Thank you!arrow_forwardQ2: Write a c++ program that reads a text file that has a poem written in it. (Write down the poem yourself in a text file manually). Your program should display that poem and count a total number of lines in the file.arrow_forward
- (True/False): A file specification includes both a file path and a file name.arrow_forwardQ6) write a M-file to calculate (N!; where N E N); if the user entered a negative number, the program should print an error message and ask the user to enter positive number again?arrow_forwardHow do you create an object for reading data from file test.txt? How do you create an object for writing data to file test.txt?arrow_forward
- please I need the best answer Write the code that opens an output file with the filename fifty.txt, uses a loop to write the numbers 1 through 50 to the file, and closes the file.arrow_forwardFill-in-the-Blank What happens if you open an output file and the file already exists?arrow_forwardSee attached images C++arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education