java - 使用 HashMap 读取/写入 txt 文件 - 小修复

标签 java file hash

代码:

public class test1 {


public static void main(String[] args) throws IOException {
    //declare reader and writer
    BufferedReader reader = null;
    PrintWriter writer = null;

    //hash maps to store the data
    HashMap<String, String> names = new HashMap<String, String>();
    HashMap<String, String> names1 = new HashMap<String, String>();


    //read the  first file and store the data
    reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IRStudents.txt"))));
    String line;
    String[] arg;
    while ((line = reader.readLine()) != null) {
        if (!line.startsWith("-")) {
            arg = line.split(" ");


            names.put(arg[0], arg[1]);


        }
    }
    reader.close();

    //read the second file, merge the data and output the data to the out file
        writer = new PrintWriter(new FileOutputStream(new File("File_2.txt")));
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR101.txt"))));
        while((line = reader.readLine()) != null){
            arg = line.split(" ");
            writer.println(arg[0] + " " + names.get(arg[0]));
            writer.println("Marks: " + arg[1] + "    Marks2:");
            writer.println("- - - - - -");    }

              //read the third, file, merge the data and output the data to the out file
               writer = new PrintWriter(new FileOutputStream(new File("File_2.txt")));
               reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR102.txt"))));
               while((line = reader.readLine()) != null){
                   arg = line.split(" ");
                   writer.println(arg[0] + " " + names.get(arg[0]));
                   writer.println("Marks: "  + "    Marks2:" + arg[1]);
                   writer.println("- - - - - -");    }


        writer.flush();
        writer.close();
        reader.close();
    }

}

到目前为止,这段代码的作用是从文本文件中读取、存储/格式化它并写入新的文本文件。

输出文本文件看起来像这样:

25987 Alan
Marks:     Marks2:20.7
- - - - - -
25954 Betty
Marks:     Marks2:63.4
- - - - - -
25218 Helen
Marks:     Marks2:53.4
- - - - - -

现在这与标记相距甚远。每个学生都有两个不同的分数,存储在不同的文本文件中。我想从两个文本文件导入标记信息并输出到一个文本文件。

代码仅从第二个 (IR102) 文件获取标记,而不是 (IR101) 文件。我假设是因为第二个代码覆盖了

我需要再创建一个新的哈希值吗?

PS:这样的话,用这个方法是否可以得到两个分数的平均值呢?

---附加代码:

 //So this is the IRStudents file. This contains Student ID and Name.
 25987 Alan
 25954 Betty
 25654 Chris
 25622 David
 //So the StudentID has to match all the student Id with the mark files.

 //File 1 Mark 1 with marks in it. 
 25987 35.6
 25954 70.2
 25654 58.6
 25622 65.0

 //File 2 Mark 2 with marks in it. 
 25987 20.7
 25954 63.4
 25654 35.1
 25622 57.8

 //So the output file should be.

 25987 Alan
 Marks1: 35.6 Mark2: 20.7  AverageMark: (35.6+20.7)/2
 - - - - - - - -  - - -  - - - - - -  - - - - - - - - 
 25954 Betty
 Marks1: 70.2 Mark2: 63.4 AverageMark:(average)
 - - - - - -  - - -  - - - - - - - - - - - - - - - - 

还有更多内容

最佳答案

首先,您重新打开并写入 File_2.txt 两次,第二次打开会清除第一次打开的结果。相反,您希望在读取第二个和第三个文件时存储数据,然后在最后输出。

处理此存储的一种简洁方法是创建一个名为 Student 的新类,该类将包含 ID、名称和标记列表。

请注意,如果您想计算平均值,使用 double 等数字类型表示标记可能最有意义。

import java.util.*;
import java.io.*;

public class test1 {
    static class Student {
        String id;
        String name;
        List<Double> marks;

        public Student(String id, String name) {
            this.id = id;
            this.name = name;
            marks = new ArrayList<Double>();
        }
        public void addMark(Double d) {
            marks.add(d);
        }
        public void writeToPW(PrintWriter out) {
            out.println(id + " " + name);
            double d = 0;
            for (int i = 0; i < marks.size(); i++) {
                out.printf("Marks%d: %.2f ", (i+1), marks.get(i));
                d += marks.get(i);
            }
            out.println("AverageMark: " + d / marks.size());
            out.println("- - - - - -");    
        }
    }
    public static void main(String[] args) throws IOException {
        //declare reader and writer
        BufferedReader reader = null;
        PrintWriter writer = null;

        //hash maps to store the data
        HashMap<String, Student> students = new HashMap<String, Student>();

        // list to maintain the original order
        List<Student> orderedStudents = new ArrayList<Student>();


        //read the  first file and store the data
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IRStudents.txt"))));
        String line;
        String[] arg;
        while ((line = reader.readLine()) != null) {
            if (!line.startsWith("-")) {
                arg = line.split(" ");
                Student student = new Student(arg[0], arg[1]);
                students.put(arg[0], student);
                orderedStudents.add(student);
            }
        }
        reader.close();


        reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR101.txt"))));
        while((line = reader.readLine()) != null){
            arg = line.split(" ");
            students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
        }


        reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR102.txt"))));
        while((line = reader.readLine()) != null){
            arg = line.split(" ");
            students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
        }

        // Now we can do writing.
        writer = new PrintWriter(new FileOutputStream(new File("File_2.txt")));
        for (Student s: orderedStudents) {
            s.writeToPW(writer);
        }
        writer.close();
    }
}

关于java - 使用 HashMap 读取/写入 txt 文件 - 小修复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29269487/

相关文章:

java - 完美的Java代码来确定Double是否是完全平方?

java - Elasticsearch 查询不返回任何内容

c - 使用 getc 从文件读取并使用 putc 打印

python - Python 中的 MD5 哈希

ruby - 如何获取 Ruby 哈希以删除输出中的大括号

java - 为什么 Java 会尝试从堆中保留最大空间

java - 使用 Ajax 发布 JSON 数组并使用 Gson 在 servlet 中解析

java - 用Java制作多个文件

iphone - 在 iPhone 模拟器上读取本地文件会崩溃,但在真实设备上不会崩溃

ruby - 如何计算哈希数组中的值