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.
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...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line,...
1 Design and implement FileCompare program that compares two text input files (file1.txt and file2.txt), line-by-line, for equality. Print any lines that are not equivalent indicating the line numbers in both files. The language of implementation is java 2 . Create a program that reads a string input from the user, then determines and prints how many of each lowercase vowels (a, e. i, o, and u) appear in the entire string. Have a separate counter for each vowel. Also...
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
Solve the following using java Write a program that runs three threads, each thread randomizes a...
Solve the following using java Write a program that runs three threads, each thread randomizes a number between 1 and 100. The main thread waits for all the others to finish, calculates the maximum of the numbers, which were randomized, and prints it. Random number 1: 10 Random number 2: 38 Random number 3: 81 Max: 81 Note: You should create 3 threads in adition to the main thread. Also, you can use a single thread class and create 3...
Python problem: write a main function that ask user for 2 file name, open files and...
Python problem: write a main function that ask user for 2 file name, open files and read the 1st line of each and compare them using hamming function(below is the hamming function I wrote). Just assume the fist line of each file only contains 0s and 1s, such as 0100111101. The main function may have to do some extra work to remove newline characters or other whitespace from the text read from each file. This is hamming function that need...
Write a function called char_counter that counts the number of a certain character in a text...
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...
C++ See the provided specification files. Complete the implementation for each as a separate file. void...
C++ See the provided specification files. Complete the implementation for each as a separate file. void seen(std::string); If there is already a Word object in the Words list, then the number of occurrences for this word is incremented. If there is no Word object for this word already, create a new word object with occurrence =1, and insert this object into the list of Word objects. std::string getNextWord(); Returns the next word of the list and sets the currentItem pointer...