java文件操作

package com.softeem.exercise;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.RandomAccessFile;

public class TestStudentRandomAccessFile {

	public void outputStudent(String path,Student stu){//向文件中写入学生信息
		FileOutputStream fps = null;
		try {
			fps=new FileOutputStream(path,true);
			PrintStream ps = null;
			String s = stu.toString()+"\n";
//			byte[] buffer=s.getBytes();
//			System.out.write(buffer,0,buffer.length);
			ps = new PrintStream(fps);
			ps.print(s);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void inputStudent(String path){//文件信息输入到内存中
		RandomAccessFile raf =null;
		File file = new File(path);
		try {
			raf=new RandomAccessFile(file,"rw");
				System.out.println(raf.readLine());	
				
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public String getKeybordString(){//获取从键盘输入的姓名
		String s=null;
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br= new BufferedReader(isr);
		System.out.println("请输入学生姓名:");
		try {
			s=br.readLine();
			if(s==null){
				s="";			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return s;
		
	}
	
	
	public int getKeybordInt(){//获取从键盘输入的年龄
		int age=0;
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		System.out.println("请输入学生年龄");
		try {
			age=Integer.parseInt(br.readLine());
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return age;
	}
	
	public void createStudentFile(String path){//创建文件
		String[] s=path.split("student.txt");
		File dir = new File(s[0]);
		//先创建目录
		if(!(dir.exists())){
			dir.mkdir();
		}
		File file = new File(dir.getAbsolutePath()+"/"+"student.txt");
		if(!(file.exists())){
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public Student getStudent(String name,int age){
		Student stu = new Student(name,age);
		return stu;
	}
	
	public static void main(String[] args) {
		TestStudentRandomAccessFile trf = new TestStudentRandomAccessFile();
		String name="";
		int age=0;
		Student stu = new Student();
		String path="d://src/student.txt";
//		若 path 文件不存在,则创建该文件
		trf.createStudentFile(path);
//		for(int i=0;i<2;i++){
//			name=trf.getKeybordString();
//			age=trf.getKeybordInt();
//			stu = trf.getStudent(name, age);
//			trf.outputStudent(path, stu);
//		}
		System.out.println("现在将学生的信息从文件中打印 :");
		trf.inputStudent(path);
	}

}


Student类:
package com.softeem.exercise;


public class Student {
    private String name;
    private int age;
    
    public Student(String name,int age){
    	this.name=name;
    	this.age=age;
    }
    
    public Student(){
    	
    }
    
	public int getAge() {
		return age;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	public String toString(){
		return name+"   "+Integer.toString(age);
	}
    
}




你可能感兴趣的:(java)