使用JAVA的文件输入输出流编写一个记事本程序,能实现编辑、保存、另存为、查找和替换等功能。
时间: 2024-02-07 14:02:02 浏览: 30
很高兴回答您的问题。首先,我需要说明的是,这里提供的是一个基本的记事本程序,您可以根据自己的需求进行修改和优化。
Java提供了一些类来读写文件,比如File、FileReader、FileWriter、BufferedReader、BufferedWriter等。这里我们使用FileReader和FileWriter类来读写文件。
以下是一个简单的Java记事本程序的实现,包括编辑、保存、另存为、查找和替换等功能。
```java
import java.io.*;
public class Notepad {
private static File file;
private static boolean isSaved = false;
private static BufferedReader reader;
private static BufferedWriter writer;
private static String content;
public static void main(String[] args) {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String command;
System.out.println("Welcome to Notepad!");
while (true) {
System.out.print("Enter a command (open, edit, save, saveas, find, replace, quit): ");
try {
command = input.readLine();
} catch (IOException e) {
System.out.println("Error reading command.");
continue;
}
if (command.equalsIgnoreCase("open")) {
open();
} else if (command.equalsIgnoreCase("edit")) {
edit();
} else if (command.equalsIgnoreCase("save")) {
save();
} else if (command.equalsIgnoreCase("saveas")) {
saveAs();
} else if (command.equalsIgnoreCase("find")) {
find();
} else if (command.equalsIgnoreCase("replace")) {
replace();
} else if (command.equalsIgnoreCase("quit")) {
if (!isSaved) {
System.out.print("Do you want to save changes? (y/n): ");
try {
command = input.readLine();
} catch (IOException e) {
System.out.println("Error reading command.");
continue;
}
if (command.equalsIgnoreCase("y")) {
save();
}
}
System.out.println("Goodbye!");
break;
} else {
System.out.println("Invalid command.");
}
}
}
private static void open() {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter filename: ");
try {
String filename = input.readLine();
file = new File(filename);
reader = new BufferedReader(new FileReader(file));
String line;
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null) {
sb.append(line).append(System.lineSeparator());
}
content = sb.toString();
System.out.println("File opened successfully.");
isSaved = true;
} catch (IOException e) {
System.out.println("Error opening file.");
}
}
private static void edit() {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter text (type 'exit' to stop editing): ");
StringBuilder sb = new StringBuilder();
try {
String line;
while (!(line = input.readLine()).equalsIgnoreCase("exit")) {
sb.append(line).append(System.lineSeparator());
}
content = sb.toString();
isSaved = false;
} catch (IOException e) {
System.out.println("Error editing text.");
}
}
private static void save() {
if (file == null) {
saveAs();
} else {
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.close();
System.out.println("File saved successfully.");
isSaved = true;
} catch (IOException e) {
System.out.println("Error saving file.");
}
}
}
private static void saveAs() {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter filename: ");
try {
String filename = input.readLine();
file = new File(filename);
writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
writer.close();
System.out.println("File saved successfully.");
isSaved = true;
} catch (IOException e) {
System.out.println("Error saving file.");
}
}
private static void find() {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter search string: ");
try {
String search = input.readLine();
int index = content.indexOf(search);
if (index == -1) {
System.out.println("String not found.");
} else {
System.out.println("String found at position " + index + ".");
}
} catch (IOException e) {
System.out.println("Error finding string.");
}
}
private static void replace() {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter search string: ");
try {
String search = input.readLine();
System.out.print("Enter replacement string: ");
String replace = input.readLine();
content = content.replace(search, replace);
isSaved = false;
} catch (IOException e) {
System.out.println("Error replacing string.");
}
}
}
```
希望能帮到您,如有疑问请随时提出。