java文件操作

本文介绍了一个Java程序,通过使用FileOutputStream、RandomAccessFile和BufferedReader类,实现了对学生信息的读取和写入操作。程序首先创建一个名为student.txt的文件,然后通过控制台输入学生的姓名和年龄,将这些信息写入文件中,并最终从文件中读取并打印学生信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值