文件结构
![image.png](https://dn-simplecloud.shiyanlou.com/courses/uid1166923-20210115-1610681107815)
Student类
public class Student {
private String name;
private int score;
public Student(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public int getScore() {
return score;
}
public void setName(String name) {
this.name = name;
}
public void setScore(int score) {
this.score = score;
}
@Override
public String toString() {
return name + " " + score ;
}
}
Judge类
import java.io.*;
public class Judge {
// 读文件
public String[] readerFile(String uri) {
BufferedReader br = null;
String line ;
StringBuilder tmpStr = new StringBuilder();
String []arr = null;
try{
br = new BufferedReader(new FileReader(uri));
while((line = br.readLine()) != null){
tmpStr.append(line).append(",");
}
arr = tmpStr.toString().split(",");
} catch (Exception e) {
e.printStackTrace();
} finally{
try{
br.close();
}catch (IOException e){
e.printStackTrace();
}
}
return arr;
}
public void writeFile(String[] fileList,String uri){
String[] result = readerFile(uri);
int len = result.length;
for(int i = 0;i < fileList.length;i++){
int cnt = 0;
String[] answer = readerFile("自动批卷/第一册/"+fileList[i]);
for(int j = 0;j < len;j++){
if(result[j].equalsIgnoreCase(answer[j])) {
cnt++;
}
}
BufferedWriter bw = null;
try{
FileWriter fw = new FileWriter("自动批卷/第一册考试成绩.txt",true);
bw = new BufferedWriter(fw);
Student s =new Student(fileList[i].substring(0,fileList[i].lastIndexOf(".")),cnt*(100/len));
bw.write(String.valueOf(s));
bw.newLine();
bw.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try{
bw.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
File file = new File("自动批卷/第一册");
String[] fileList = file.list();
Judge judge = new Judge();
judge.writeFile(fileList,"自动批卷/标准答案.txt");
}
}
运行结果
![image.png](https://dn-simplecloud.shiyanlou.com/courses/uid1166923-20210115-1610681197563)