文件输入输出实验代码_第1页
文件输入输出实验代码_第2页
文件输入输出实验代码_第3页
文件输入输出实验代码_第4页
文件输入输出实验代码_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、eader;import java.io.DataOutputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import javax.swing.JFrame;import javax.swing.JButton;import javax.swing.JPanel;import javax.swing.JTextArea;import javax.swing.JTextField;i

3、mport java.io.DataInputStream;public class shiyan12 public static void main(String args) thisplay t=new thisplay();t.setSize(600,400);t.setVisible(true);t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);class thisplay extends JFrameJTextField field,field1;JTextArea display;JButton b1,b2,b3,b4;FileOut

4、putStream fo;FileInputStream fi;DataOutputStream d0;DataInputStream di;JPanel p;public thisplay()b1=new JButton("输入");b2=new JButton("输出");b3=new JButton("输入");b4=new JButton("输出");display=new JTextArea();field=new JTextField(10);field1=new JTextField(10);p=ne

5、w 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.addActionListener(l);b3.addActionListener(l);b4.addActionListener(l);class Listener implements ActionListener public void action

6、Performed(ActionEvent e) if(e.getSource()=b1)tryfo=new FileOutputStream("E:file.dat"); d0=new DataOutputStream(fo);d0.writeChars(field.getText();d0.close();catch(IOException ex)System.out.println(ex); if(e.getSource()=b2) try fi=new FileInputStream("E:file.dat"); DataInputStream

7、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(fo); d0.writeChars(field1.getText(); d0.close(); catch(IOException ex)System.out.println(

8、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) System.out.println(ex); 代码2:package baobao;import java.awt.BorderLayout;impor

9、t 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.FileReader;import java.io.IOException;import java.io.PrintStream;import javax.swing.JFrame;import j

10、avax.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 t=new thisplay();t.setSize(600,400);t.setVisible(true);t.setDefaultCloseOperation(JFrame.EXIT_ON_CL

11、OSE);class thisplay extends JFrameJTextField field,field1;JTextArea display;JButton b1,b2,b3,b4;FileOutputStream fo;FileInputStream fi;PrintStream d0;DataInputStream di;JPanel p;public thisplay()b1=new JButton("输入");b2=new JButton("输出");b3=new JButton("输入");b4=new JButt

12、on("输出");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.addActionListener(l);b3.addAct

13、ionListener(l);b4.addActionListener(l);public JFrame getContainer()return this;class Listener implements ActionListenerFileDialog fd; public void actionPerformed(ActionEvent e) if(e.getSource()=b1)try fd=new FileDialog(getContainer(),"是否保存?",FileDialog.SAVE); fd.setVisible(true); FileOutpu

14、tStream 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); BufferedRea

15、der 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) if(e.getSource()=b3) try fd=new FileDialog(getContainer(),"是否保存?",FileDialog.SAVE); fd.

16、setVisible(true); FileOutputStream out=new FileOutputStream(fd.getDirectory()+fd.getFile()+".txt"); String str = field1.getText(); out.write(str.getBytes(); out.close(); catch(IOException ex)System.out.println(ex); if(e.getSource()=b4) try fd=new FileDialog(getContainer(),"是否打开?"

17、,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) 代码3:package baoo;import java.awt.BorderLayout;

18、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.IOException;import java.io.InputStreamReader;import java.io.PrintStream;import javax.swing.JFrame;import javax.swing.JBut

19、ton;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 t=new thisplay();t.setSize(600,400);t.setVisible(true);t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);class this

20、play extends JFrameJTextField field,field1;JTextArea display;JButton b1,b2,b3,b4;FileOutputStream fo;FileInputStream fi;PrintStream d0;DataInputStream di;JPanel p;public thisplay()b1=new JButton("输入");b2=new JButton("输出");b3=new JButton("输入");b4=new JButton("输出&quo

21、t;);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.addActionListener(l);b3.addActionListener(l);b4.addActionListener(l);class Listener implements ActionListener public void actionPerformed(ActionEvent e) if(e.getSource()=b1)tryfo=new FileOutputStream("E:file.dat"); d0=new PrintStream(fo);d0.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论