Java文件读写

读写一个班级人员的信息,包括学号,姓名,年龄成绩。


//主类 读写
package text_Example;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class TestRandom {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file=new File("F:\\JAVA\\lpf.JAVA\\Text_File\\16140112.txt");
		RandomAccessFile raf=null;
		Student stu;
		List<Student> list=new LinkedList<Student>();
		list.add(new Student(1614011101,"李四",20,new double[]{99,55,77}));
		list.add(new Student(1614011102,"王老五",21,new double[]{98,75,97}));
		list.add(new Student(1614011103,"赵二",19,new double[]{92,56,78}));
		list.add(new Student(1614011104,"张三",20,new double[]{99,80,77}));
		Iterator ite=list.iterator();
		byte[] name =new byte[12];
		byte[] nametemp;
		double score[]=new double[3];
		//一下开始写入
		try{
			raf=new RandomAccessFile(file,"rw");
			while(ite.hasNext()){
				stu=(Student) ite.next();
				raf.writeInt(stu.getNumber());
				nametemp=stu.getName().getBytes();//将字符串形式转化为Byte数组形式;
				System.arraycopy(nametemp,0,name,0,nametemp.length);
				/*
				将Byte数组name拷贝给nametemp数组,
				方法System.arraycopy(要拷贝的数组,要拷贝的起始位置,目标数组,目标数组接收的位置,拷贝长度);
				*/
				raf.write(name);
				raf.writeInt(stu.getAge());
				score=stu.getScore();
				for(int i=0;i<score.length;i++){
					raf.writeDouble(score[i]);
				}
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			try{
				if(raf!=null)//如果文件不为空 关闭
					raf.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
		readStudent(file);
		/*readStudent(file);是依次读出数据的方法
		 *searchStudent(file,3);是读取目标文件的方法
		*/
		
	}

	//下面开始依次读出文件
	private static void readStudent(File file) {
		// TODO Auto-generated method stub
		RandomAccessFile raf=null;
		Student stu;
		long length;
		long position=0;//记录数据位置
		int number;
		String name;
		byte[] nametemp=new byte[12];
		int age;
		double score []=new double[3];
		try {
			raf=new RandomAccessFile(file,"r");
			raf.seek(0);//找到要读取数据的起始位置
			length=raf.length();
			while(position<length){
				number=raf.readInt();
				raf.readFully(nametemp);//读出Byte的方法
				name=new String(nametemp,0,nametemp.length);
				age=raf.readInt();
				for(int i=0;i<score.length;i++){
					score[i]=raf.readDouble();
				}
				stu=new Student(number,name.trim(),age,score);//将读取的数据传给Student对象
				stu.printStudent();//打印数据
				position=raf.getFilePointer();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try{
				if(raf!=null)
					raf.close();
			}catch(IOException e){
				e.printStackTrace();
			}
		}
	}
	//下面开始读取目标文件
	public static void searchStudent(File file,int num){
		RandomAccessFile raf=null;
		Student stu;
		long length;
		long position=0;
		int number;
		String name;
		byte[] nametemp=new byte[12];
		int age;
		double score[]=new double[3];
		try {
			raf=new RandomAccessFile(file,"r");
			raf.seek((num-1)*44);//找到要读取数据的起始位置
			/*
			 * 数据开头位置为 (要读取数据组数-1)*一组数据大小
			 */
			number=raf.readInt();
			raf.readFully(nametemp);
			name=new String(nametemp,0,nametemp.length);
			age=raf.readInt();
			for(int i=0;i<score.length;i++){
				score[i]=raf.readDouble();
			}
			stu=new Student(number,name.trim(),age,score);
			stu.printStudent();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
		

}


}

//学生类
package text_Example;

import java.util.Arrays;

public class Student {
	private int number;
	private String name;
	private int age;
	private double score[];
	public Student(int number, String name, int age, double[] score) {
		super();
		this.number = number;
		this.name = name;
		this.age = age;
		this.score = score;
	}
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public double[] getScore() {
		return score;
	}
	public void setScore(double[] score) {
		this.score = score;
	}
	
	public int compareTo(Object o){
		return this.age-((Student)o).getAge();
	}
	public String toString(){
		return name+"的年龄是:"+age;
	}
	public void printStudent(){
		System.out.println(name+"的学号是:"+number+",年龄是:"+age+",各科成绩是"+Arrays.toString(score));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值