Java File Operations and Word Count
Java File Operations and Word Count
To create multiple files, modify the code by iterating over a list of filenames. For each iteration, instantiate a new File object with the directory path and filename, and use FileWriter to create and write to each file. Enclose the file creation and writing logic inside a loop and customize the file names dynamically or from input .
This method is simplistic and might inaccurately count words or sentences because it assumes spaces exclusively separate words and lines exclusively represent sentences. It does not account for punctuation marks separating words or sentences spanning multiple lines, which could lead to inaccuracies in word or sentence count .
Preemptive multitasking allows the operating system to allocate processor time across multiple programs automatically by dividing time into slices, improving responsiveness and system stability. In contrast, cooperative multitasking depends on each program to yield control, which can lead to issues if a program fails to do so, as seen in older systems like 16-bit Windows .
The code involves several steps: (1) creating a File object pointing to the desired directory and invoking mkdir() to create the directory, (2) creating another File object for the intended file path, (3) using FileWriter to open this file for writing, and (4) writing the content with write() followed by closing the stream with close().
Time-sharing is crucial because it ensures multiple processes receive a portion of processor time, enabling concurrent execution of programs and maximizing CPU utilization. It facilitates fair allocation of processing power, prevents a single process from monopolizing the CPU, and enhances the system's overall efficiency .
BufferedReader reads text from a character-based input stream efficiently by buffering characters, which reduces the number of I/O operations by reading larger chunks of data at once. This buffering results in improved performance, especially for large files, compared to reading each character or line individually .
The code uses FileWriter to write a string, defined in contentString, to a file named 'essay.txt'. Later, it uses FileReader and BufferedReader to read the content of the file line by line and print it to the console. FileWriter writes data to the file using fWriter.write(contentString);, and BufferedReader reads data with a while loop, checking each line with br.readLine() until null is returned .
Enhancements include modularizing the code by separating tasks into methods, using consistent naming conventions, adding comments to explain the logic, handling exceptions gracefully with try-catch blocks rather than throws, and refactoring repetitive code into utility functions to improve readability and maintainability .
The code splits contentString by spaces into an array using the split(" ") method to estimate the number of words, which is stored in no_of_words. For sentence count, it reads each line using BufferedReader and increments a counter variable 'count' by 1 for each line read, assuming each line represents a separate sentence .
The code uses the java.io.File class to create a new directory named 'AyushFolder' using the mkdir() method. It then creates a new text file 'essay.txt' inside this directory using the File class combined with FileWriter. The Folder is created first with File file = new File("C:\\AyushFolder"); file.mkdir(); and then the file with File objFile = new File("C:\\AyushFolder\\"+fileString); FileWriter fWriter = new FileWriter(objFile);.