0% found this document useful (0 votes)
5 views

JAVA

The document contains multiple Java programs that perform various file operations, including counting words, replacing words, finding the longest word, counting lines, and copying content between files. Each program is accompanied by code snippets and instructions for testing with a sample text file. Additionally, there is an example of a Java Swing application that demonstrates key listener functionality.

Uploaded by

ahmadmalik5088
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)
5 views

JAVA

The document contains multiple Java programs that perform various file operations, including counting words, replacing words, finding the longest word, counting lines, and copying content between files. Each program is accompanied by code snippets and instructions for testing with a sample text file. Additionally, there is an example of a Java Swing application that demonstrates key listener functionality.

Uploaded by

ahmadmalik5088
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
You are on page 1/ 4

1.

Count Total Words in a File


This program reads a file and counts the total number of words.

java
Copy code
import java.io.*;

public class WordCount {


public static void main(String[] args) {
String fileName = "sample.txt"; // File to be read
int wordCount = 0;

try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {


String line;

// Read file line by line


while ((line = br.readLine()) != null) {
String[] words = line.split("\\s+"); // Split by spaces
wordCount += words.length; // Count words in the line
}

System.out.println("Total words in the file: " + wordCount);

} catch (FileNotFoundException e) {
System.out.println("File not found: " + fileName);
} catch (IOException e) {
System.out.println("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 java.io.*;

public class ReplaceWordInFile {


public static void main(String[] args) {
String inputFile = "sample.txt"; // Original file
String outputFile = "updated.txt"; // 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 = br.readLine()) != null) {
// Replace the word
String updatedLine = line.replace(oldWord, newWord);
bw.write(updatedLine);
bw.newLine();
}

System.out.println("Word replacement complete. Check the file: " +


outputFile);

} catch (IOException e) {
System.out.println("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 java.io.*;

public class LongestWordInFile {


public static void main(String[] args) {
String fileName = "sample.txt";
String longestWord = "";

try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {


String line;

while ((line = br.readLine()) != null) {


String[] words = line.split("\\s+");
for (String word : words) {
if (word.length() > longestWord.length()) {
longestWord = word;
}
}
}

System.out.println("The longest word in the file is: " + longestWord);

} catch (FileNotFoundException e) {
System.out.println("File not found: " + fileName);
} catch (IOException e) {
System.out.println("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 java.io.*;

public class LineCount {


public static void main(String[] args) {
String fileName = "sample.txt";
int lineCount = 0;

try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {


while (br.readLine() != null) {
lineCount++;
}

System.out.println("Total lines in the file: " + lineCount);


} catch (FileNotFoundException e) {
System.out.println("File not found: " + fileName);
} catch (IOException e) {
System.out.println("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 java.io.*;

public class FileCopy {


public static void main(String[] args) {
String sourceFile = "sample.txt";
String destinationFile = "copy.txt";

try (BufferedReader br = new BufferedReader(new FileReader(sourceFile));


BufferedWriter bw = new BufferedWriter(new
FileWriter(destinationFile))) {

String line;

while ((line = br.readLine()) != null) {


bw.write(line);
bw.newLine();
}

System.out.println("File copied successfully to: " + destinationFile);

} catch (FileNotFoundException e) {
System.out.println("Source file not found: " + sourceFile);
} catch (IOException e) {
System.out.println("Error copying the file.");
}
}
}
How to Test:
Create a file sample.txt with sample content.
Compile and run each program with the appropriate file name.
Example sample.txt Content:

kotlin
Copy code
Java is fun. Learning Java makes programming easier.
Python is also a powerful language.
Enjoy experimenting with file handling!

import javax.swing.*;
import java.awt.event.*;

public class KeyListenerExample {


public static void main(String[] args) {
JFrame frame = new JFrame("KeyListener Example");
JTextField textField = new JTextField();

textField.setBounds(50, 50, 200, 30);


textField.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
System.out.println("Key Typed: " + e.getKeyChar());
}

@Override
public void keyPressed(KeyEvent e) {
System.out.println("Key Pressed: " + e.getKeyChar());
}

@Override
public void keyReleased(KeyEvent e) {
System.out.println("Key Released: " + e.getKeyChar());
}
});

frame.add(textField);
frame.setSize(300, 200);
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}

You might also like