}
}
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)在线全文阅读。
相关推荐: