展开全部
操作数据库才用到Driver接口吧,文件读写似乎不需要用它
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.util.Scanner;
/**
* @author idle~`32313133353236313431303231363533e59b9ee7ad9431333264643162
* @version 0.1
* @date 2008-12-14
*/
public class IOTest {
// 1
public static void readFile(String fileName){
try{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
while(line != null){
System.out.println(line);
line = br.readLine();
}
br.close();
fr.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void selectStudent(String sid,String fileName){
try{
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
boolean flag = false;
while(line != null){
if(line.contains(sid)){
System.out.println(line);
flag = true;
}
line = br.readLine();
}
if(!flag)
System.out.println("No date found");
br.close();
fr.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void addStudent(String sid,String name,String cid,String email,String phone,String fileName){
try{
RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
String line = rf.readLine();
boolean flag = false;
while(line != null){
if(line.contains(sid)){
System.out.println("ID repeated");
flag = true;
}
line = rf.readLine();
}
if(!flag){
rf.seek(rf.length());
rf.writeChars(sid + " " + name + " " + cid + " " + email + " " + phone + "\n");
}
rf.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void updateStudent(String sid,String name,String cid,String email,String phone,String fileName){
try{
RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
String line = rf.readLine();
StringBuffer sb = new StringBuffer();
boolean flag = false;
while(line != null){
if(line.contains(sid)){
flag = true;
sb.append(sid + " " + name + " " + cid + " " + email + " " + phone + "\n");
}else{
sb.append(line + "\n");
}
line = rf.readLine();
}
if(!flag){
System.out.println("ID is null");
}else{
rf.writeChars(sb.toString());
System.out.println("----update successfully----");
}
rf.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void deleteStudent(String sid,String fileName){
try{
RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
String line = rf.readLine();
StringBuffer sb = new StringBuffer();
boolean flag = false;
while(line != null){
if(line.contains(sid)){
flag = true;
}else
sb.append(line + "\n");
line = rf.readLine();
}
if(!flag){
System.out.println("ID is null");
}else{
rf.writeChars(sb.toString());
System.out.println("----delete successfully----");
}
rf.close();
}catch(Exception e){
e.printStackTrace();
}
}
public static void delete(String fileName){
try{
RandomAccessFile rf=new RandomAccessFile(fileName,"rw");
rf.setLength(0);
System.out.println("----empty----");
rf.close();
}catch(Exception e){
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
String fileName = "D:\\list.dat";
StringBuffer sb = new StringBuffer();
sb.append("------------------------------\n");
sb.append("1.列出所有数据\n");
sb.append("2.查询一行数据通过学生的学号\n");
sb.append("3.添加数据\n");
sb.append("4.修改一行数据通过学生的学号\n");
sb.append("5.删除一行数据通过学生的学号\n");
sb.append("6.删除所有的数据\n");
sb.append("7.退出\n");
sb.append("------------------------------");
Scanner scanner = new Scanner(System.in);
boolean t = true;
while(t){
System.out.println(sb.toString());
int flag = scanner.nextInt();
switch(flag){
case 1:
IOTest.readFile(fileName);
break;
case 2:
System.out.print("Student id:");
String sid = scanner.next();
IOTest.selectStudent(sid, fileName);
break;
case 3:
System.out.print("Student id:");
String sid3 = scanner.next();
System.out.print("\nStudent name:");
String name3 = scanner.next();
System.out.print("\nclass id:");
String cid3 = scanner.next();
System.out.print("\nemail:");
String email3 = scanner.next();
System.out.print("\nphone:");
String phone3 = scanner.next();
IOTest.addStudent(sid3, name3, cid3, email3, phone3, fileName);
break;
case 4:
System.out.print("Student id:");
String sid4 = scanner.next();
System.out.print("\nStudent name:");
String name4 = scanner.next();
System.out.print("\nclass id:");
String cid4 = scanner.next();
System.out.print("\nemail:");
String email4 = scanner.next();
System.out.print("\nphone:");
String phone4 = scanner.next();
IOTest.updateStudent(sid4, name4, cid4, email4, phone4, fileName);
break;
case 5:
System.out.print("Student id:");
String sid5 = scanner.next();
IOTest.deleteStudent(sid5,fileName);
break;
case 6:
System.out.println("are you sure(Y/N)?");
String s = scanner.next();
if(s.equalsIgnoreCase("Y")){
IOTest.delete(fileName);
}
break;
case 7:
t = false;
break;
}
}
}
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起