大神啊 帮忙写个Java程序啊 要用RandomAccessFile类写
1个回答
展开全部
package file;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class RandomSort
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
File file = new File("D:/score.txt");
File file2 = new File("D:/sorted_score.txt");
try
{
if(file.exists())
{
file.delete();
file.createNewFile();
}
RandomAccessFile ran = new RandomAccessFile(file, "rw");
for(int i=1;i<3;i++)
{
System.out.println("请输入第"+i+"个学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学号:");
String stuNo = sc.nextLine();
System.out.println("请输入成绩:");
int score = sc.nextInt();
sc.nextLine();
ran.write(("姓名:"+name+",学号:"+stuNo+",成绩:"+score+"\r\n").getBytes());
}
sc.close();
List<Student> list = new ArrayList<Student>();
String s;
ran.seek(0);
while((s=ran.readLine())!=null){
String stu = new String(s.getBytes("iso-8859-1"),"utf-8");
String name = stu.substring(stu.indexOf("姓名:")+3, stu.indexOf(","));
String stuNo = stu.substring(stu.indexOf("学号:")+3, stu.lastIndexOf(","));
String score = stu.substring(stu.indexOf("成绩:")+3);
Student student = new Student();
student.setName(name);
student.setScore(Integer.parseInt(score));
student.setStuNo(stuNo);
list.add(student);
}
ran.close();
Collections.sort(list,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore()-o1.getScore();
}
});
RandomAccessFile access = new RandomAccessFile(file2, "rw");
for(int i=0;i<list.size();i++)
{
Student stu = list.get(i);
access.write(stu.toString().getBytes());
}
access.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class Student {
private String name;
private String stuNo;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "姓名:"+name+",学号:"+stuNo+",成绩:"+score+"\r\n";
}
}
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class RandomSort
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
File file = new File("D:/score.txt");
File file2 = new File("D:/sorted_score.txt");
try
{
if(file.exists())
{
file.delete();
file.createNewFile();
}
RandomAccessFile ran = new RandomAccessFile(file, "rw");
for(int i=1;i<3;i++)
{
System.out.println("请输入第"+i+"个学生姓名:");
String name = sc.nextLine();
System.out.println("请输入学号:");
String stuNo = sc.nextLine();
System.out.println("请输入成绩:");
int score = sc.nextInt();
sc.nextLine();
ran.write(("姓名:"+name+",学号:"+stuNo+",成绩:"+score+"\r\n").getBytes());
}
sc.close();
List<Student> list = new ArrayList<Student>();
String s;
ran.seek(0);
while((s=ran.readLine())!=null){
String stu = new String(s.getBytes("iso-8859-1"),"utf-8");
String name = stu.substring(stu.indexOf("姓名:")+3, stu.indexOf(","));
String stuNo = stu.substring(stu.indexOf("学号:")+3, stu.lastIndexOf(","));
String score = stu.substring(stu.indexOf("成绩:")+3);
Student student = new Student();
student.setName(name);
student.setScore(Integer.parseInt(score));
student.setStuNo(stuNo);
list.add(student);
}
ran.close();
Collections.sort(list,new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getScore()-o1.getScore();
}
});
RandomAccessFile access = new RandomAccessFile(file2, "rw");
for(int i=0;i<list.size();i++)
{
Student stu = list.get(i);
access.write(stu.toString().getBytes());
}
access.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
class Student {
private String name;
private String stuNo;
private int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStuNo() {
return stuNo;
}
public void setStuNo(String stuNo) {
this.stuNo = stuNo;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return "姓名:"+name+",学号:"+stuNo+",成绩:"+score+"\r\n";
}
}
更多追问追答
追问
可以运行吗亲爱的 我很急
追答
(⊙﹏⊙)
不能运行我给你写那么多干嘛
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询