请用Java语言写一个记事本,功能有读入文本,保存文本,删除文本,记录文本的创建日期,记录文本个数

时间: 2023-09-03 09:25:47 浏览: 63
### 回答1: 以下是 Java 语言实现的记事本代码: ``` import java.io.*; import java.util.*; public class Notebook { private List<String> notes = new ArrayList<>(); private Map<String, Date> createDates = new HashMap<>(); // 读入文本 public void readNote(String fileName) throws IOException { BufferedReader br = new BufferedReader(new FileReader(fileName)); String line; while ((line = br.readLine()) != null) { notes.add(line); createDates.put(line, new Date()); } br.close(); } // 保存文本 public void saveNote(String fileName) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); for (String note : notes) { bw.write(note); bw.newLine(); } bw.close(); } // 删除文本 public void deleteNote(String note) { notes.remove(note); createDates.remove(note); } // 记录文本的创建日期 public Date getCreateDate(String note) { return createDates.get(note); } // 记录文本个数 public int getNoteCount() { return notes.size(); } } ``` ### 回答2: 以下是使用Java语言编写一个记事本的示例代码: ``` import java.io.*; import java.util.Date; public class NotePad { private int count; public NotePad() { count = 0; } public void readNote() { try { File file = new File("note.txt"); BufferedReader reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { System.out.println("读取文本失败"); } } public void saveNote(String content) { try { File file = new File("note.txt"); FileWriter writer = new FileWriter(file, true); BufferedWriter bw = new BufferedWriter(writer); bw.write(content); bw.newLine(); bw.close(); writer.close(); count++; } catch (IOException e) { System.out.println("保存文本失败"); } } public void deleteNote() { File file = new File("note.txt"); if (file.exists()) { if (file.delete()) { count--; System.out.println("成功删除文本"); } else { System.out.println("删除文本失败"); } } else { System.out.println("文件不存在"); } } public void getCreationDate() { File file = new File("note.txt"); if (file.exists()) { Date creationDate = new Date(file.lastModified()); System.out.println("文本创建日期:" + creationDate); } else { System.out.println("文件不存在"); } } public int getNoteCount() { return count; } public static void main(String[] args) { NotePad notePad = new NotePad(); notePad.saveNote("第一条笔记"); notePad.saveNote("第二条笔记"); notePad.readNote(); notePad.getCreationDate(); notePad.deleteNote(); } } ``` 上述代码实现了记事本的基本功能。其中,readNote方法用于读取文本内容。saveNote方法用于保存文本内容,并且在保存完成后将计数器count加1。deleteNote方法用于删除文本文件,并且在删除成功后将计数器count减1。getCreationDate方法用于获取文本的创建日期。getNoteCount方法用于获取记事本中的文本个数。在main方法中,我们演示了如何使用这些功能函数。 ### 回答3: 以下是一个用Java语言实现的简易记事本的示例代码: ```java import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; public class NotePad { private int noteCount; public NotePad() { this.noteCount = 0; } public void readNote() { try { BufferedReader reader = new BufferedReader(new FileReader("note.txt")); String line; // 从文件中逐行读取文本内容 while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } public void saveNote(String text){ try { BufferedWriter writer = new BufferedWriter(new FileWriter("note.txt", true)); // 获取当前日期 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentDate = dateFormat.format(new Date()); // 将文本内容追加到文件末尾 writer.write(currentDate + ":" + text); writer.newLine(); writer.close(); noteCount++; } catch (IOException e) { e.printStackTrace(); } } public void deleteNote() { File file = new File("note.txt"); if(file.exists()) { file.delete(); noteCount = 0; System.out.println("删除文本成功"); }else { System.out.println("文本文件不存在"); } } public int getNoteCount() { return noteCount; } public static void main(String[] args) { NotePad notePad = new NotePad(); notePad.saveNote("第一行文字"); notePad.saveNote("第二行文字"); notePad.readNote(); System.out.println("总共有 " + notePad.getNoteCount() + " 个文本"); notePad.deleteNote(); System.out.println("总共有 " + notePad.getNoteCount() + " 个文本"); } } ``` 运行以上代码,可以实现以下功能: 1. readNote方法用于读取保存的文本并打印到控制台。 2. saveNote方法用于将输入的文本保存到note.txt文件中,并在文本前添加创建日期。 3. deleteNote方法用于删除note.txt文件。 4. getNoteCount方法返回保存的文本个数。 5. 主函数中的示例代码演示了如何使用上述方法。 6. 运行结果如下: ``` 2022-01-01 10:30:00:第一行文字 2022-01-01 10:31:00:第二行文字 总共有 2 个文本 删除文本成功 总共有 0 个文本 ```

相关推荐

最新推荐

recommend-type

用 Windows “记事本”创建一个文本文件,其中每行包含一段英文,试读出文件的全部内容,并判断:(1)该文本文件共有多少行?(2)文件中以大写字母P开头的有多少行?(3)一行包含字

题目:用 Windows “记事本”创建一个文本文件,其中每行包含一段英文,试读出文件的全部内容,并判断: (1)该文本文件共有多少行? (2)文件中以大写字母P开头的有多少行? (3)一行中包含字符最多的及包含字母...
recommend-type

基于Swing应用程序设计:一个简易的文本编辑框

一个简易的文本编辑框super("记事本"); JMenuBar mBar=new JMenuBar(); setJMenuBar(mBar); JMenu []m={new JMenu("文件"),new JMenu("帮助")}; JMenuItem [][]mI={ {new JMenuItem("新建"),new ...
recommend-type

###对华为OD分布式操作系统的详细介绍

华为OD
recommend-type

2110220116吴骏博.py

2110220116吴骏博.py
recommend-type

基于Java的ApplicationPower快速项目生成脚手架设计源码

ApplicationPower项目生成脚手架设计源码:该项目基于Java开发,包含284个文件,主要使用Java和Shell语言。ApplicationPower是一个快速的项目生成脚手架,旨在帮助开发者快速搭建项目框架,包括创建项目结构、配置文件、开发环境等,提高开发效率。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。