要求:编写一个模拟日记本的程序,通过在控制台输入指令,实现在本地新建文件,打开日记本和修改日记本等功能。
指令1代表“新建日记本”,可以从控制台获取用户输入的日记内容
指令2代表“打开日记本”,读取指定路径的TXT文件的内容并输出到控制台
指令3代表“修改日记本”,修改日记本中原有的内容
指令4代表保存
指令5代表退出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
import java.io.*; import java.util.Scanner; public class IO_日记本 { /** * 模拟日记本程序 */ private static String filePath; private static String message = "" ; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); while ( true ) { System.out.println( "---------日记本---------" ); System.out.println( "1.写日记" ); System.out.println( "2.查看日记" ); System.out.println( "3.修改日记" ); System.out.println( "4.保存" ); System.out.println( "5.退出" ); System.out.println( "注意:每次输入内容后记得保存!" ); System.out.print( "请输入操作指令:" ); int command = sc.nextInt(); switch (command) { case 1 : // 1:新建文件(写日记) createFile(); break ; case 2 : // 2:打开文件(查看日记) openFile(); break ; case 3 : // 3:修改文件 editFile(); break ; case 4 : // 4:保存 saveFile(); break ; case 5 : // 5:退出 System.out.println( "谢谢使用本系统,欢迎下次再来!" ); System.exit( 0 ); break ; default : System.out.println( "您输入的指令错误!" ); break ; } } } //写一个方法写入文件内容 private static void createFile() { message = "" ; //新建文件时,暂存文件内容清空 Scanner sc = new Scanner(System.in); System.out.println( "请输入内容,停止编写请输入:stop" ); StringBuffer stb = new StringBuffer(); //用于后期输入内容的拼接 String inputMessage = "" ; while (!inputMessage.equals( "stop" )) { //输入stop则停止 if (stb.length() > 0 ) { stb.append( "\r\n" ); //追加换行符 } stb.append(inputMessage); //拼接输入信息 inputMessage = sc.nextLine(); //获取输入信息 } message = stb.toString(); //将输入内容缓存 } //写一个方法保存文件 private static void saveFile() throws Exception { FileWriter out = new FileWriter( "文件路径" , true ); out.write(message + "\r\n" ); //将输入内容写入 message = "" ; out.close(); } //写一个方法打开文件 public static void openFile() throws Exception { Reader r = new FileReader( "文件路径" ); BufferedReader br = new BufferedReader(r); // char[] c = new char[1024];//定义一个桶装每次读取多少个数据 // int len; // while ((len = br.read(c)) != -1) {//读取所有内容,如果读完所有内容则停止 // String str = new String(c, 0, len);//每次读取0到len的所有内容 // System.out.print(str);//因为读取时会自动换行所以这里我们不需要换行 // } String line; while ((line = br.readLine()) != null ) { //一次读取一行 System.out.println(line); //因为读取一行时程序不会自己换行,所以这里我们需要给它换行 } //System.out.println();//读完换行 } //写一个方法修改文件 /** * 替换 * @throws IOException */ public static void editFile() throws IOException{ Scanner sc = new Scanner(System.in); //原有的内容 System.out.println( "原文件内容:" ); String str1 =sc.next();; //要替换的内容 System.out.println( "修改成:" ); String str2 =sc.next(); // 读取 File file = new File( "文件路径" ); FileReader in = new FileReader(file); BufferedReader buf = new BufferedReader(in); //缓冲流 // 内存流, 作为临时流 CharArrayWriter tempStream = new CharArrayWriter(); // 替换 String line = null ; while ( (line = buf.readLine()) != null ) { // 替换每行中, 符合条件的字符串 line = line.replaceAll(str1, str2); //正则表达式 // 将该行写入内存 tempStream.write(line+ "\r\n" ); } // 关闭输入流 buf.close(); // 将内存中的流写入文件 FileWriter fw = new FileWriter(file); tempStream.writeTo(fw); fw.close(); } } |
到此这篇关于Java利用IO流实现简易的记事本功能的文章就介绍到这了,更多相关Java记事本内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/qq_62731133/article/details/124260290