zoukankan      html  css  js  c++  java
  • 用IO流实现自动批卷

    文件结构

    image.png

    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

    ljm要加油
  • 相关阅读:
    Codeforces Round #544 (Div. 3) C. Balanced Team
    Codeforces Round #544 (Div. 3) B.Preparation for International Women's Day
    Codeforces Round #544 (Div. 3) A.Middle of the Contest
    HDU-2647-Reward
    2015.3.15
    USACO Section 5.1 Musical Themes(枚举)
    [STOI2014]舞伴(dp)
    USACO Section 5.1 Fencing the Cows(凸包)
    2015.3.10
    USACO Section 4.3 Street Race(图的连通性+枚举)
  • 原文地址:https://www.cnblogs.com/ljmmm1/p/14281222.html
Copyright © 2011-2022 走看看