用java写一个记事本程序,拥有文件打开、保存、编辑、查找的功能
时间: 2024-05-15 09:18:11 浏览: 149
用java写的记事本程序
好的,我可以提供一个简单的Java记事本程序的示例代码,具有文件打开、保存、编辑和查找功能。请注意,此代码仅用于学习和参考,有关生产级别代码和安全性的考虑,请参考相关文档和实践。
```java
import java.io.*;
import java.util.Scanner;
public class Notepad {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String filename = "";
String line = "";
while (true) {
// menu
System.out.println("Choose an option:");
System.out.println("1 - Open file");
System.out.println("2 - Save file");
System.out.println("3 - Edit file");
System.out.println("4 - Find text");
System.out.println("0 - Exit");
// read option from user input
int option = scanner.nextInt();
scanner.nextLine(); // consume the newline character
switch (option) {
case 1:
// open file
System.out.print("Enter filename: ");
filename = scanner.nextLine();
try {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedReader.close();
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
break;
case 2:
// save file
System.out.print("Enter filename: ");
filename = scanner.nextLine();
try {
FileWriter fileWriter = new FileWriter(filename);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
while (!(line = scanner.nextLine()).equals(":wq")) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
bufferedWriter.close();
} catch (IOException e) {
System.out.println("Error writing file: " + e.getMessage());
}
break;
case 3:
// edit file
System.out.print("Enter filename: ");
filename = scanner.nextLine();
// read the file into a StringBuilder object
StringBuilder sb = new StringBuilder();
try {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
sb.append(System.lineSeparator());
}
bufferedReader.close();
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
break;
}
// edit the content
System.out.println("Edit the content. Enter ':wq' to save and exit.");
String content = sb.toString();
int index = content.lastIndexOf(System.lineSeparator());
System.out.print(content.substring(0, index + 1));
String newContent = "";
while (!(line = scanner.nextLine()).equals(":wq")) {
newContent += line + System.lineSeparator();
}
newContent += System.lineSeparator();
// save the edited content back to the file
try {
FileWriter fileWriter = new FileWriter(filename);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(newContent + content.substring(index + 1));
bufferedWriter.close();
} catch (IOException e) {
System.out.println("Error writing file: " + e.getMessage());
}
break;
case 4:
// find text
System.out.print("Enter filename: ");
filename = scanner.nextLine();
System.out.print("Enter text to find: ");
String text = scanner.nextLine();
try {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
int lineNumber = 1;
boolean found = false;
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(text)) {
System.out.println("Found at line " + lineNumber + ": " + line);
found = true;
}
lineNumber++;
}
bufferedReader.close();
if (!found) {
System.out.println("Text not found in the file.");
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
break;
case 0:
// exit
System.out.println("Bye!");
System.exit(0);
break;
default:
System.out.println("Invalid option. Please choose again.");
break;
}
}
}
}
```
这个程序基于命令行,它可以使用Java的标准输入输出和文件读写API,实现简单的文件处理功能。在文件打开时,可以按行读取文件中的内容并输出到控制台。在文件保存时,可以将用户在命令行输入的内容写入文件中。在文件编辑时,它会先将文件读取到StringBuilder对象中,再在命令行中提示用户输入要编辑的内容。当用户输入“:wq”时,编辑完成,并将所做的更改写回到原始文件中。在查找文件时,可以读取文件并逐行搜索包含指定文本的行。
阅读全文