try {
String filename = JOptionPane.showInputDialog("Enter filename");
BufferedReader input = new BufferedReader(new FileReader(filename));
String line = input.readLine();
while (line != null) {
System.out.println(line);
line = input.readLine();
}
input.close();
} catch (FileNotFoundException exc) {
System.out.println("Could not open file for read");
} catch (IOException exc) {
System.out.println("Could not read input from open file");
}
try {
String filename = JOptionPane.showInputDialog("Enter filename");
PrintWriter output = new PrintWriter(new FileWriter(filename));
String userText = JOptionPane.showInputDialog("Enter line");
while (userText != null) {
output.println(userText);
userText = JOptionPane.showInputDialog("Enter line");
}
output.close();
} catch (IOException exc) {
System.out.println("Could not open file for write");
}