实验七输入输出流
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验七输入输出流
一、实验目的
(1)了解流式输入输出的基本概念;
(2)熟悉Java.io包中常用的基本输入输出类;
(3)掌握程序与文件之间的基本输入输出操作;
二、实验内容
1) 把字符串“20102324,张三,男,25,软件工程”,保存到文件”d:\\a.txt”中,
并读取打印a.txt文件中的内容。
2) 把我们在聊天界面中发送的消息保存到日志文件里面,在界面上添加一个历史按钮,当点击历史按钮时读取日志文件内容。
三、实验步骤
1)把字符串“20102324,张三,男,25,软件工程”,保存到文件”d:\\a.txt”中,并读取打印a.txt文件中的内容。
(1) 定义变量message,值为“20102324,张三,男,25,软件工程”;
(2) 创建指向”D:\\a.txt”的文件对象
(3) 创建输出流
(4) 把message写入流(文件)中
(5) 关闭输出流
(6) 创建输入流
(7) 读取文件内容
(8) 打印文件内容
(9) 关闭输入流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileIO {
public static void main(String[] args) {
String message = "20102324,张三,男,25,软件工程";
File myFile=new File("D:\\a.txt");
//写文件
try {
FileOutputStream fout = new FileOutputStream(myFile,true);//不覆盖
try {
fout.write(message.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();}}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//读文件
try {
FileInputStream fint = new FileInputStream(myFile);
byte b[] = new byte[(int) myFile.length()];
try {
fint.read(b);
String s = new String(b);
System.out.println(s);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
fint.close();
} catch (IOException e) {
e.printStackTrace();
}}}
catch (FileNotFoundException e) {
e.printStackTrace();}}}
2)把我们在聊天界面中发送的消息保存到日志文件里面,在界面上添加一个历史按钮,当点击历史按钮时读取日志文件内容。
(1) 编写聊天界面,添加发送按钮的鼠标点击事件
private void initUI() {
// 用户名的标签
JLabel la_name = new JLabel("接收到的消息:");
JLabel la_users = new JLabel("发送给:");
final JTextField jtf_sned = new JTextField(20);// 发送输入框
javax.swing.JButton bu_send = new javax.swing.JButton("Send");
//添加一个历史按钮
javax.swing.JButton bu_history = new javax.swing.JButton("历史");
//添加好友
jcb_users.addItem("钱尧");
jcb_users.addItem("徐丹");
this.add(la_name);
this.add(jta_recive);
this.add(la_users);
this.add(jtf_sned);
this.add(jcb_users);
this.add(bu_send);
this.add(bu_history);
// 发送事件监听器
ActionListener sendListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String reciver = (String) jcb_users.getSelectedItem();
reciver = reciver.trim();// 去除空格
String content = jtf_sned.getText();
// 发送一条聊天消息
String message = userName+"对"+reciver+"说:"+content+"\r\n";
jta_recive.append(message);//显示到界面
jtf_sned.setText("");
writeLog(message);}};
bu_send.addActionListener(sendListener);
jtf_sned.addActionListener(sendListener);
//为历史按钮添加事件
bu_history.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
jta_recive.append(" 历史记录\r\n");
String message = readLog();
jta_recive.append(message);
jta_recive.append(" \n\n");}});}
(2) 在事件处理方法中把聊天信息写入文件,注意不能覆盖文件里面已有的信息。File myFile=new File("D:\\a.txt");
//保存聊天记录
p rivate void writeLog(String message){
File logFile=new File("D:\\a.txt");
//写文件
try {
FileOutputStream fout = new FileOutputStream(logFile,true);//不覆盖
try {
fout.write(message.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally{