672编号文件输入输出实验代码

上传人:玩*** 文档编号:145281975 上传时间:2020-09-18 格式:PDF 页数:15 大小:333.45KB
返回 下载 相关 举报
672编号文件输入输出实验代码_第1页
第1页 / 共15页
672编号文件输入输出实验代码_第2页
第2页 / 共15页
672编号文件输入输出实验代码_第3页
第3页 / 共15页
672编号文件输入输出实验代码_第4页
第4页 / 共15页
672编号文件输入输出实验代码_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《672编号文件输入输出实验代码》由会员分享,可在线阅读,更多相关《672编号文件输入输出实验代码(15页珍藏版)》请在金锄头文库上搜索。

1、实 验 报 告实 验 报 告 课程名称 java 程序设计实用教程 课程名称 java 程序设计实用教程 实验名称实验名称 输入输出文件件处理 实验类型实验类型 设计型 实验地点实验地点 机房 304 实验日期实验日期 2012-3-28 指导教师指导教师 某某人 专 业专 业 计算机科学与技术 班 级班 级 算机 1003 学 号学 号 * 姓 名姓 名 * 成 绩 成 绩 辽宁石油化工大学计算机与通信工程学辽宁石油化工大学计算机与通信工程学 一实验目的:一实验目的: 二实验要求:二实验要求: 三、程序代码三、程序代码: 代码代码 1: package bao; import java.aw

2、t.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import javax.swing.J

3、Frame; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import java.io.DataInputStream; public class shiyan12 public static void main(String args) thisplay t=new thisplay(); t.setSize(600,400); t.setVisible(true); t.setDefaultCloseOp

4、eration(JFrame.EXIT_ON_CLOSE); class thisplay extends JFrame JTextField field,field1; JTextArea display; JButton b1,b2,b3,b4; FileOutputStream fo; FileInputStream fi; DataOutputStream d0; DataInputStream di; JPanel p; public thisplay() b1=new JButton(输入输入); b2=new JButton(输出输出); b3=new JButton(输入输入)

5、; b4=new JButton(输出输出); display=new JTextArea(); field=new JTextField(10); field1=new JTextField(10); p=new JPanel(); Listener l=new Listener(); /* p.add(field); p.add(b1); p.add(b2); p.add(field1); p.add(b3); p.add(b4); add(p,BorderLayout.NORTH); add(display); b1.addActionListener(l); b2.addActionL

6、istener(l); b3.addActionListener(l); b4.addActionListener(l); class Listener implements ActionListener public void actionPerformed(ActionEvent e) if(e.getSource()=b1) try fo=new FileOutputStream(E:file.dat); d0=new DataOutputStream(fo); d0.writeChars(field.getText(); d0.close(); catch(IOException ex

7、)System.out.println(ex); if(e.getSource()=b2) try fi=new FileInputStream(E:file.dat); DataInputStream di= new DataInputStream(fi); display.setText(di.readLine(); di.close(); catch(Exception ex)ex.printStackTrace(); if(e.getSource()=b3) try fo=new FileOutputStream(E:file.dat); d0=new DataOutputStream

8、(fo); d0.writeChars(field1.getText(); d0.close(); catch(IOException ex)System.out.println(ex); if(e.getSource()=b4) try fi=new FileInputStream(E:file.dat); /di=new DataInputStream(fi); BufferedReader d =new BufferedReader(new InputStreamReader(fi); display.setText(d.readLine(); catch(IOException ex)

9、 System.out.println(ex); 代码代码 2: package baobao; import java.awt.BorderLayout; import java.awt.FileDialog; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReade

10、r; import java.io.IOException; import java.io.PrintStream; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; import java.io.DataInputStream; public class shiyan public static void main(String args) thisplay

11、t=new thisplay(); t.setSize(600,400); t.setVisible(true); t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); class thisplay extends JFrame JTextField field,field1; JTextArea display; JButton b1,b2,b3,b4; FileOutputStream fo; FileInputStream fi; PrintStream d0; DataInputStream di; JPanel p; public thi

12、splay() b1=new JButton(输入); b2=new JButton(输出); b3=new JButton(输入); b4=new JButton(输出); display=new JTextArea(); field=new JTextField(10); field1=new JTextField(10); p=new JPanel(); Listener l=new Listener(); /* p.add(field); p.add(b1); p.add(b2); p.add(field1); p.add(b3); p.add(b4); add(p,BorderLay

13、out.NORTH); add(display); b1.addActionListener(l); b2.addActionListener(l); b3.addActionListener(l); b4.addActionListener(l); public JFrame getContainer() return this; class Listener implements ActionListener FileDialog fd; public void actionPerformed(ActionEvent e) if(e.getSource()=b1) try fd=new F

14、ileDialog(getContainer(),是否保存? ,FileDialog.SAVE); fd.setVisible(true); FileOutputStream out=new FileOutputStream(fd.getDirectory()+fd.getFile()+.txt); String str = field.getText(); out.write(str.getBytes(); out.close(); catch (Exception ess) if(e.getSource()=b2) try fd=new FileDialog(getContainer(),是否打开? ,FileDialog.LOAD); fd.setVisible(true); BufferedReader in = new BufferedReader(new FileReader(fd.getDirectory() + fd.getFile(); String str = null; while (str = in.readLine() != null) display.append(str + n); in.close(); catch (Exception ess) i

展开阅读全文
相关资源
相关搜索

当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号