77范文网 - 专业文章范例文档资料分享平台

Java实验指导(4)-io流(2)

来源:网络收集 时间:2020-05-13 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:或QQ: 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

}

}

rf.seek (【代码1】); rf.writeInt(47);

rf.writeDouble(47.00021); rf.close();

rf = new RandomAccessFile(\ for (int i = 0; i < 10; i++) { ;

System.out.println(\ +【代码2】); }

rf.【代码3】;

} catch (IOException e) {

System.out.println(e.toString()); }

import java.io.*;

public class testRandom2 {

public static void main(String args[]) {

try { }

RandomAccessFile rf = new RandomAccessFile(\, \); for (int i = 0; i < 10; i++) { }

rf.close();

rf = new RandomAccessFile(\, \); rf.seek (0); rf.writeInt(47);

rf.writeDouble(47.00021); rf.close();

rf = new RandomAccessFile(\, \); for (int i = 0; i < 10; i++) { }

rf.close();

System.out.println(e.toString());

;

System.out.println(\ + rf.readInt() + \

+rf.readDouble());

rf.writeInt(i);

rf.writeDouble(i * 1.414);

} catch (IOException e) {

6

}

}

7. 下面的程序使用对象的序列化及对象流进行对象的保存和读取,请按如下的程序模板中的要求,将【代码】替换为Java程序代码,以完成该程序。 程序清单:

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException;

import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;

public class SerializableDemo {

public static void main(String args[]) throws Exception {

student stu = new student(20131064, \张三\西华大学\ FileOutputStream fout = new FileOutputStream(\ ObjectOutputStream oout = new 【代码1】; // 输出对象

oout.writeObject(stu); oout.close(); stu = null;

FileInputStream fin = new 【代码2】; ObjectInputStream oin = new 【代码3】; // 读入对象

stu = 【代码4】; oin.close();

System.out.println(\学生信息:\ System.out.println(\: \ System.out.println(\: \ System.out.println(\: \

System.out.println(\:\ } }

class student implements Serializable { int id;

String name; int age;

String department;

public student(int id, String name, int age, String department) { this.id = id; this.name = name;

7

this.age = age;

this.department = department; } }

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable;

public class SerializableDemo { private static student stu;

public static void main(String args[]) throws Exception { stu = new student(20131064, \张三\, 20, \西华大学\); FileOutputStream fout = new FileOutputStream(\); ObjectOutputStream oout = new ObjectOutputStream(fout); oout.writeObject(stu); oout.close(); stu = null;

FileInputStream fin = new FileInputStream(\); ObjectInputStream oin = new ObjectInputStream(fin);

stu =new student(20131064, \张三\, 20, \西华大学\); oin.close();

System.out.println(\学生信息:\);

System.out.println(\+stu.id);

System.out.println(\: \+ stu.name); System.out.println(\: \ + stu.age);

System.out.println(\:\ + stu.department); }

}

class student implements Serializable { int id; String name; int age;

String department;

public student(int id, String name, int age, String department) { this.id = id;

this.name = name;

8

}

}

this.age = age;

this.department = department;

8. 下面的程序以字节为单位进行文件的读写操作,请阅读并分析该程序,上机运行查看效果,然后修改程序,使其以字符流方式进行文件的读写操作。 程序清单:

import java.io.*; import java.awt.*;

import java.awt.event.*; import javax.swing.*;

public class readerbyte extends JFrame implements ActionListener {

JButton b1, b2, b3; JTextField jt1; JTextArea jt2;

readerbyte() {

setTitle(\演示\ getContentPane().setLayout(new FlowLayout()); this.setDefaultLookAndFeelDecorated(true); this.setSize(450, 350); b1 = new JButton(\写入\ b2 = new JButton(\读取\ b3 = new JButton(\退出\ b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this);

jt1 = new JTextField(10);

jt2 = new JTextArea(10, 30); jt2.setLineWrap(true);

jt2.setWrapStyleWord(true);

JScrollPane jspa = new JScrollPane(jt2);

JPanel p1 = new JPanel();

p1.add(new JLabel(\请输入文件名:\ p1.add(jt1);

JPanel p2 = new JPanel();

p2.add(new JLabel(\文件内容:\

9

p2.add(jspa);

JPanel p3 = new JPanel(); p3.add(b1); p3.add(b2); p3.add(b3);

getContentPane().add(p1); getContentPane().add(p2); getContentPane().add(p3); setVisible(true);

}

public void actionPerformed(ActionEvent ee) { if (ee.getSource() == b3) { dispose();

System.exit(0); } if (ee.getSource() == b1)

writer();

if (ee.getSource() == b2) reader(); }

void writer() { String s1, s2;

s1 = jt1.getText().trim(); if (s1.equals(\

JOptionPane.showMessageDialog(null, \没有输入文件名\ jt1.requestFocus(); } else {

s2 = jt2.getText().trim(); char s22[] = s2.toCharArray(); try {

FileOutputStream file = new FileOutputStream(s1); for (int i = 0; i < s22.length; i++) file.write(s22[i]); file.close();

} catch (IOException ie) { }

jt1.setText(\ jt2.setText(\

10

百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库Java实验指导(4)-io流(2)在线全文阅读。

Java实验指导(4)-io流(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.77cn.com.cn/wenku/jiaoyu/1050264.html(转载请注明文章来源)
Copyright © 2008-2022 免费范文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ: 邮箱:tiandhx2@hotmail.com
苏ICP备16052595号-18
× 注册会员免费下载(下载后可以自由复制和排版)
注册会员下载
全站内容免费自由复制
注册会员下载
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: