(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
Modern Database Management
Starting Out with Python (4th Edition)
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Starting Out with C++ from Control Structures to Objects (9th Edition)
Elementary Surveying: An Introduction To Geomatics (15th Edition)
- 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(Remove text) Write a program that removes all the occurrences of a specified string from a text file. (For example, my mother went to buy groceries while John and I play Call of duty.) removes the string John from the file. You should create a file with a paragraph. removes the string "John and" from the file. Note:- Please write a java code and also need an output for this program. (Also, let me know with what name file should be saved to get output)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_forwardMake 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_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