| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 791 人关注过本帖
标题:[求助]java.io包里面有iomethods这个方法吗?
取消只看楼主 加入收藏
beyondbetty
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2006-11-9
收藏
 问题点数:0 回复次数:0 
[求助]java.io包里面有iomethods这个方法吗?

书上的一段程序
import java.io.*;

public class Database {
private RandomAccessFile database;
private String fName = new String();;
private IOmethods io = new IOmethods();
Database() throws IOException {
System.out.print("File name: ");
fName = io.readLine();
}
private void add(DbObject d) throws IOException {
database = new RandomAccessFile(fName,"rw");
database.seek(database.length());
d.writeToFile(database);
database.close();
}
private void modify(DbObject d) throws IOException {
DbObject[] tmp = new DbObject[1];
d.copy(tmp);
database = new RandomAccessFile(fName,"rw");
while (database.getFilePointer() < database.length()) {
tmp[0].readFromFile(database);
if (tmp[0].equals(d)) {
tmp[0].readFromConsole();
database.seek(database.getFilePointer()-d.size());
tmp[0].writeToFile(database);
database.close();
return;
}
}
database.close();
System.out.println("The record to be modified is not in the database");
}
private boolean find(DbObject d) throws IOException {
DbObject[] tmp = new DbObject[1];
d.copy(tmp);
database = new RandomAccessFile(fName,"r");
while (database.getFilePointer() < database.length()) {
tmp[0].readFromFile(database);
if (tmp[0].equals(d)) {
database.close();
return true;
}
}
database.close();
return false;
}
private void printDb(DbObject d) throws IOException {
database = new RandomAccessFile(fName,"r");
while (database.getFilePointer() < database.length()) {
d.readFromFile(database);
d.writeLegibly();
System.out.println();
}
database.close();
}
public void run(DbObject rec) throws IOException {
String option;
System.out.println("1. Add 2. Find 3. Modify a record; 4. Exit");
System.out.print("Enter an option: ");
option = io.readLine();
while (true) {
if (option.charAt(0) == '1') {
rec.readFromConsole();
add(rec);
}
else if (option.charAt(0) == '2') {
rec.readKey();
System.out.print("The record is ");
if (find(rec) == false)
System.out.print("not ");
System.out.println("in the database");
}
else if (option.charAt(0) == '3') {
rec.readKey();
modify(rec);
}
else if (option.charAt(0) != '4')
System.out.println("Wrong option");
else return;
printDb(rec);
System.out.print("Enter an option: ");
option = io.readLine();
}
}
}

搜索更多相关主题的帖子: java iomethods private database fName 
2006-11-18 00:14
快速回复:[求助]java.io包里面有iomethods这个方法吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018050 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved