1.
Count Total Words in a File
This program reads a file and counts the total number of words.
java
Copy code
import [Link].*;
public class WordCount {
public static void main(String[] args) {
String fileName = "[Link]"; // File to be read
int wordCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
// Read file line by line
while ((line = [Link]()) != null) {
String[] words = [Link]("\\s+"); // Split by spaces
wordCount += [Link]; // Count words in the line
}
[Link]("Total words in the file: " + wordCount);
} catch (FileNotFoundException e) {
[Link]("File not found: " + fileName);
} catch (IOException e) {
[Link]("Error reading the file: " + fileName);
}
}
}
2. Replace a Word in a File
This program replaces a specific word in a file and writes the updated content to a
new file.
java
Copy code
import [Link].*;
public class ReplaceWordInFile {
public static void main(String[] args) {
String inputFile = "[Link]"; // Original file
String outputFile = "[Link]"; // File with replaced words
String oldWord = "Java";
String newWord = "Python";
try (BufferedReader br = new BufferedReader(new FileReader(inputFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile))) {
String line;
// Read the file line by line
while ((line = [Link]()) != null) {
// Replace the word
String updatedLine = [Link](oldWord, newWord);
[Link](updatedLine);
[Link]();
}
[Link]("Word replacement complete. Check the file: " +
outputFile);
} catch (IOException e) {
[Link]("Error processing the file.");
}
}
}
3. Find the Longest Word in a File
This program identifies the longest word in a file.
java
Copy code
import [Link].*;
public class LongestWordInFile {
public static void main(String[] args) {
String fileName = "[Link]";
String longestWord = "";
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String line;
while ((line = [Link]()) != null) {
String[] words = [Link]("\\s+");
for (String word : words) {
if ([Link]() > [Link]()) {
longestWord = word;
}
}
}
[Link]("The longest word in the file is: " + longestWord);
} catch (FileNotFoundException e) {
[Link]("File not found: " + fileName);
} catch (IOException e) {
[Link]("Error reading the file: " + fileName);
}
}
}
4. Count Lines in a File
This program counts the total number of lines in a file.
java
Copy code
import [Link].*;
public class LineCount {
public static void main(String[] args) {
String fileName = "[Link]";
int lineCount = 0;
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
while ([Link]() != null) {
lineCount++;
}
[Link]("Total lines in the file: " + lineCount);
} catch (FileNotFoundException e) {
[Link]("File not found: " + fileName);
} catch (IOException e) {
[Link]("Error reading the file: " + fileName);
}
}
}
5. Copy Content from One File to Another
This program copies the content of one file to another file.
java
Copy code
import [Link].*;
public class FileCopy {
public static void main(String[] args) {
String sourceFile = "[Link]";
String destinationFile = "[Link]";
try (BufferedReader br = new BufferedReader(new FileReader(sourceFile));
BufferedWriter bw = new BufferedWriter(new
FileWriter(destinationFile))) {
String line;
while ((line = [Link]()) != null) {
[Link](line);
[Link]();
}
[Link]("File copied successfully to: " + destinationFile);
} catch (FileNotFoundException e) {
[Link]("Source file not found: " + sourceFile);
} catch (IOException e) {
[Link]("Error copying the file.");
}
}
}
How to Test:
Create a file [Link] with sample content.
Compile and run each program with the appropriate file name.
Example [Link] Content:
kotlin
Copy code
Java is fun. Learning Java makes programming easier.
Python is also a powerful language.
Enjoy experimenting with file handling!
import [Link].*;
import [Link].*;
public class KeyListenerExample {
public static void main(String[] args) {
JFrame frame = new JFrame("KeyListener Example");
JTextField textField = new JTextField();
[Link](50, 50, 200, 30);
[Link](new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
[Link]("Key Typed: " + [Link]());
}
@Override
public void keyPressed(KeyEvent e) {
[Link]("Key Pressed: " + [Link]());
}
@Override
public void keyReleased(KeyEvent e) {
[Link]("Key Released: " + [Link]());
}
});
[Link](textField);
[Link](300, 200);
[Link](null);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}