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 #
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
Get Answers For Free
Most questions answered within 1 hours.