Question

1.      Create 100 text files automatically; in each file write a random number from 1 to 10....


1.      Create 100 text files automatically; in each file write a random number from 1 to 10. Use outputstreams (fileoutputstream, buffredwriter….)

2.      Read the content of the 100 files and combine them into a 1 single file.

3.      Write java code to do the following:

a.      To write the following text into a text file EEEESAAA@23SDCFSAWERF%WASDFGHWERTRQW

b.      Read the file using a java program

c.      Find how many D’s in the file

d.      Extract the text between the @ and the #



1. Create 100 text files automatically; in each file write a random number from 1 to 10. Use outputstreams (fileoutputstream, buffredwriter….)

2. Read the content of the 100 files and combine them into a 1 single file.

3. Write java code to do the following:

a. To write the following text into a text file EEEESAAA@23SDCFSAWERF%WASDFGHWERTRQW

b. Read the file using a java program

c. Find how many D’s in the file

d. Extract the text between the @ and the #

all of this in a single program

Homework Answers

Answer #1

Find the code for the above question below, read the comments provided in the code for better understanding. If found helpful please do upvote this.
Please refer to the screenshot of the code to understand the indentation of the code.

Copyable code

import java.util.Random;

import java.io.*;

public class fileread {

    public static void main(String args[]) {

        int count = 100;// numbe rof files to create

        // Part 1

        // run a loop for 100 times so as to create 100 files

        for (int i = 1; i <= count; i++) {

            // get random integer between 1 to 10

            Random rand = new Random();

            int randomVal = rand.nextInt(10) + 1;

            // craete file name file1.txt , file2.txt and so on

            try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file" + i + ".txt"))) {

                // write the random variable value into the file

                bufferedWriter.write("" + randomVal);

            } catch (IOException e) {

                // show error if any

                System.out.println("Error : " + e.getMessage());

            }

        }

        System.out.println(count + " files created");

        // Part 2

        // run a loop for 100 times so as to read the 100 files

        String combinedData = "";

        for (int i = 1; i <= count; i++) {

            // rwad the files

            try (BufferedReader bufferedReader = new BufferedReader(new FileReader("file" + i + ".txt"))) {

                combinedData += bufferedReader.readLine() + "\n";

            } catch (IOException e) {

                // show error if any

                System.out.println("Error : " + e.getMessage());

            }

        }

        // now write teh combineddata into the file100.txt

        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file" + count + ".txt"))) {

            // write the cobined data into the file

            bufferedWriter.write(combinedData);

        } catch (IOException e) {

            // show error if any

            System.out.println("Error : " + e.getMessage());

        }

        System.out.println("Combined data of all files written in file" + count + ".txt");

        // /Question 3

        try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file_ques3.txt"))) {

            // write the value into the file

            bufferedWriter.write("EEEESAAA@23SDCFSAWERF%WASDFGHWERTRQW");

        } catch (IOException e) {

            // show error if any

            System.out.println("Error : " + e.getMessage());

        }

        // read the file content

        try (BufferedReader bufferedReader = new BufferedReader(new FileReader("file_ques3.txt"))) {

            String fileData = bufferedReader.readLine();

            // count number of D's in it

            int dCount = 0;

            for (int i = 0; i < fileData.length(); i++)

                if (fileData.charAt(i) == 'D')

                    dCount++;

            System.out.println("Number of D's int the file : " + dCount);

            // Extract the text between the @ and the #

            String extractedText = fileData.substring(fileData.indexOf('@')+1, fileData.indexOf('%'));

            System.out.println("Extracted Text : " + extractedText);

        } catch (IOException e) {

            // show error if any

            System.out.println("Error : " + e.getMessage());

        }

    }

}

Screenshot of code

Output

Files Created

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
C# Reading from Files Write a program to open a text file containing information about buildings....
C# Reading from Files Write a program to open a text file containing information about buildings. The program must display all the buildings in the file and then find the lowest cost building based on the cost per square foot. Format for one building: <building name> <size> sqft $<cost> Example: Allgood Hall 35000 sqft $8,250,099.75 Create the text file and add three or more of your favorite buildings.
● Write code to read the content of the text file input.txt using JAVA. For each...
● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is...
Program using java !! Create two files, file1.csv and file2.csv Write the following information in file1.csv:...
Program using java !! Create two files, file1.csv and file2.csv Write the following information in file1.csv: Apple 1.69 001 Banana 1.39 002 Write the following information in file2.csv: Apple 1.69 001 Carrot 1.09 003 Beef 6.99 004 You have two files, both of the two files have some information about products: • Read these two files, find out which products exist in both files, and then save these products in a new file. You can use the two files you...
Create a text file called “Trivia.txt” that contains the contents of a trivia game, where the...
Create a text file called “Trivia.txt” that contains the contents of a trivia game, where the questions and answers are on separate lines. Example: How many players are on a Baseball team? Nine What is the name of Batmans butler? Alfred Hg is the chemical symbol for what element? Mercury Create a program that reads the trivia questions from this text file, prints them out to netbeans and asks the user to input an answer. Compare the users answer to...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write a Java Program, that opens the file "students.txt" The program must read the file line...
Write a Java Program, that opens the file "students.txt" The program must read the file line by line The program parses each line that it reads For example, for this line: 1:mohamed:ali:0504123456:cs102:cs202 The program must print    >ID = 1    >First Name = Mohamed   >Last Name = Ali   >Mobie = 0504123456   >Courses = cs102, cs202 In addition, it adds the mobile phone number into an ArrayList called studentPhoneList Print the content and the size of studentPhoneList Show your results and provide...
Code a C file, following these instructions: This lab is about getting the input from a...
Code a C file, following these instructions: This lab is about getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words. Open the manual page for open() system call and add to your code...
Using Java Write a program that reads a file of numbers of type int and outputs...
Using Java Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the output file should...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different implementation. Each symbol table will contain as a key a word read from a text file and as a value the number of times that word occurs in the text file. Have the program fill the first symbol table with these counts, keeping track of how long that takes using a Stopwatch object. It then does the same thing with the second symbol table....