JAVA
JAVA
java
Copy code
import java.io.*;
} 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.*;
String line;
} 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.*;
} 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.*;
java
Copy code
import java.io.*;
String line;
} 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.*;
@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);
}
}