0% found this document useful (0 votes)
12 views2 pages

Java File Operations and Word Count

The Java program creates a folder and a text file containing an essay about multitasking operating systems. It counts the number of words in the essay and the number of sentences read from the file. Finally, it outputs these counts to the console along with the content of the essay.

Uploaded by

karshuhana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Java File Operations and Word Count

The Java program creates a folder and a text file containing an essay about multitasking operating systems. It counts the number of words in the essay and the number of sentences read from the file. Finally, it outputs these counts to the console along with the content of the essay.

Uploaded by

karshuhana
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

import [Link]; import [Link].

*;
import [Link];

public class TextFileDemo {

public static void main(String[] args) throws IOException {

//find number of words present in essay


// find number of sentences
// generate score as no_of_sentences + no_of_words
// copy the above file to a new location
// read the content to console

//File class is used to create a folder as well file

// File file = new File("C:\\Tmahindra");


// [Link]();
// [Link]("Folder created ");
//
// Scanner scanner = new Scanner([Link]);
// [Link]("enter the name of the file");
// String filename = [Link]();
//
//
// File obj = new File("C:\\Tmahindra\\"+filename+".txt");
//
// // use of file writer class
// FileWriter fWriter = new FileWriter(obj);
//
// [Link]("hi");
// [Link]();
// [Link]("file created and written");

File file = new File("C:\\AyushFolder");


[Link]();

[Link]("folder created");

Scanner scanner = new Scanner([Link]);


String fileString = "[Link]";
File objFile =new File("C:\\AyushFolder\\"+fileString);
FileWriter fWriter = new FileWriter(objFile);
String contentString = "A single-tasking system can only run one
program at a time, while a multi-tasking operating system allows more than one
program to be running concurrently.\n This is achieved by time-sharing, where the
available processor time is divided between multiple processes.\n These processes
are each interrupted repeatedly in time slices by a task-scheduling subsystem of
the operating system.\n Multi-tasking may be characterized in preemptive and
cooperative types.\n In preemptive multitasking, the operating system slices the
CPU time and dedicates a slot to each of the programs.\n Unix-like operating
systems, such as Linux—as well as non-Unix-like, such as AmigaOS—support preemptive
multitasking.\n Cooperative multitasking is achieved by relying on each process to
provide time to the other processes in a defined manner.\n 16-bit versions of
Microsoft Windows used cooperative multi-tasking; 32-bit versions of both Windows
NT and Win9x used preemptive multi-tasking.";
String []contentarr = [Link](" ");

int no_of_words = [Link];


[Link](no_of_words+"\n\n");
[Link](contentString);
[Link]();

FileReader fileReader = new FileReader(objFile);


BufferedReader br = new BufferedReader(fileReader);

String st;
int count =0;

while ((st = [Link]()) != null) {


[Link](st);
count++;
}

[Link]("\n\n\n\n no_of_words = "+no_of_words + " "+ " no of


sentences = "+ count);

Common questions

Powered by AI

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);.

You might also like