java 写入文件尾_用java输入输出流自动打开文件后如何在文件末尾追加一行字符串...

该博客展示了如何使用Java的RandomAccessFile类在文件末尾追加数据,包括读取、更新、删除和添加学生信息的示例代码。通过交互式菜单实现对学生数据的操作,实现了对文件内容的完整管理。
摘要由CSDN通过智能技术生成

展开全部

操作数据库才用到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;

}

}

}

}

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值