rf.seek(10); System.out.println("\n 选择显示的文件内容:"); String s; while ((s = rf.readLine()) != null) System.out.println(s); rf.close(); } catch (FileNotFoundException fnoe) { } catch (IOException ioe) { } } }
import java.io.*; public class JAVA_B_5_6 { public static void main(String arg[]) { try { // 添加方式创建文件输出流
4
FileOutputStream fout = new FileOutputStream("JAVA_B_5_6.txt", true); DataOutputStream dout = new DataOutputStream(fout); dout.writeInt(1); dout.writeChars("罗马" + "\n"); dout.writeInt(2); dout.writeChars("北京" + "\n"); dout.close(); } catch (IOException ioe) { } try { FileInputStream fin = new FileInputStream("JAVA_B_5_6.txt"); DataInputStream din = new DataInputStream(fin); int i = din.readInt(); while (i != -1) { // 输入流未结束时,输入流结束时i 为-1 System.out.print(i + " "); char ch; while ((ch = din.readChar()) != '\n') // 字符串未结束时 System.out.print(ch); System.out.println(); i = din.readInt(); } din.close(); } catch (IOException ioe) { } } }
import java.io.*; public class JAVA_B_5_7 implements Serializable // 序列化接口 { int bh = 1; int nl = 21; String xm; JAVA_B_5_7(int bh, String xm, int nl)// 构造方法
5
{ this.bh = bh; this.xm = xm; this.nl = nl; } JAVA_B_5_7()// 构造方法 { this(0, "", 21); } void save(String fname)// 保存到文件中的方法 { try { FileOutputStream fout = new FileOutputStream(fname);// 输出 文件流 ObjectOutputStream out = new ObjectOutputStream(fout);// 输 出对象流 out.writeObject(this); // 写入对象 out.close(); } catch (FileNotFoundException fe) { } catch (IOException ioe) { } } void display(String fname)//显示文件中对象信息的方法 { try{ FileInputStream fin = new FileInputStream(fname);//输入文件流 ObjectInputStream in = new ObjectInputStream(fin);//输入对象流 JAVA_B_5_7 OO = (JAVA_B_5_7)in.readObject(); //读取对象 System.out.println(" 类名: "+OO.getClass().getName()+" "+OO.getClass().getInterfaces()[0]); System.out.println(" "+OO.bh+" "+OO.xm+" "+OO.nl); in.close(); } catch (FileNotFoundException fe){} catch (IOException ioe){} catch (ClassNotFoundException ioe) {} } public static void main(String arg[]) { String fname = "JAVA_B_5_7.obj"; JAVA_B_5_7 O1 = new JAVA_B_5_7(1, "张驰", 14);
6
O1.save(fname); O1.display
(fname); } }
import java.util.*; class JAVA_B_5_8 { public static void main(String args[]) { Random r1 = new Random(1234567890L); Random r2 = new Random(1234567890L); boolean b = r1.nextBoolean(); // 随机数不为0 时取真值 int i1 = r1.nextInt(100); // 产生大于等于0 小于100 的随机数 int i2 = r2.nextInt(100); // 同上 double i3 = r1.nextDouble(); // 产生大于等于0.0 小于1.0 的 随机数 double i4 = r2.nextDouble(); // 同上 double d1 = Math.sin(Math.toRadians(30.0)); double d2 = Math.log(Math.E); double d3 = Math.pow(2.0, 3.0); int r = Math.round(33.6F); System.out.println("b 的随机数不为0 时 " + b); System.out.println("i1 的随机数为 " + i1); System.out.println("i2 的随机数为 " + i2); System.out.println("d1 的随机数为 " + i3); System.out.println("d2 的随机数为 " + i4); System.out.println("30 弧度的正弦值: Math.sin(Math.toRadians(30.0))= " + d1); System.out.println("E 的对数值:Math.log(Math.E)= " + d2); System.out.println("2 的3 次方:Math.pow(2.0, 3.0)= " + d3); System.out.println("33.6F 四舍五入: Math.round(33.6F)= " + r); } }
7
import java.text.*; import ja