java - HashMap 困惑。读取/写入文件。 java

标签 java file hashmap filewriter

到目前为止的代码:

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


    //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("Marks.txt"))));
while((line = reader.readLine()) != null){
    arg = line.split(" ");
    writer.println(arg[0] + " " + names.get(arg[0]));
    writer.println("Marks: " + arg[1]);
    writer.println("- - - - - -");
}                                                                          


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

因此文本文件中的输出如下所示:

 25220 Fiona
 Marks: 68.3
 - - - - - -
 25212 Greg
 Marks: 70.5
 - - - - - -       

我有另一个文本文件,其中包含另一组标记,其布局与第一个标记文件相同。

现在我想向数据集添加一组新的标记,因此它应该如下所示:

 25220 Fiona
 Marks: 68.3  Marks2: 21.2
 - - - - - -
 25212 Greg
 Marks: 70.5  Marks2: 23.43
 - - - - - -         

那么我可以做什么来添加呢?我想我必须为新的文本文档添加一个新的 HashMap ?但当我尝试做所有这些事情时,它从来没有完全发挥作用。

IR 学生:

25987 Alan
25954 Betty
25654 Chris
25622 David                                                                                                      

最佳答案

您也可以执行以下操作。

package toBeDeleted;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class MarksProcessor {


    private final Map<String, Record> map = new HashMap<>();

    public static void main(String[] args) {
        String fileName = "file1.txt"; // change it to your specific file.
        MarksProcessor marksProcessor = new MarksProcessor();
        marksProcessor.processFile(fileName, 0);
        fileName = "file2.txt";
        marksProcessor.processFile(fileName, 1);
        marksProcessor.writeData();

    }

    private void processFile(String fileName, int marksIndex) {
        try(/*specify your reader resources here*/) {
            // read the first record and get rollNumber, name and marks.
            String roll = "valueYouGot";
            double value = 0.0; // the value you read.
            Record record = map.get(roll);
            // if record is null, you need to create one
            // and put it into the map.
            //record.updateMarks(marksndex, value);
        }
    }

    private void writeData() {

        // if this needs to be written to a file/stream, create a writer.
        for (Map.Entry<String, Record> entry : map.entrySet()) {
            String roll = entry.getKey();
            Record record = entry.getValue();
            if (record != null) {
                String name = record.getName();
                double marks1 = record.getMarks(0);
                double marks2 = record.getMarks(1);
                // Now you have all the values. Print them 
                // however you like. Wherever you like.
            }
        }
    }

    static class Record {
        private String name;
        private double[] marks = new double[2];


        Record(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public double getMarks(int index) {
            if (index < 0 || index > 1)
                throw new IllegalArgumentException("index should be 0 or 1 but"
                        + " the supplied index was " + index);
            return marks[index];
        }
        public void updateMarks(int index, double value ) {
            if (index < 0 || index > 1)
                throw new IllegalArgumentException("index should be 0 or 1 but"
                        + " the supplied index was " + index);
            marks[index] = value;
        }


        @Override
        public String toString() {
            return "the way you want to type your output";
        }

    }


}

关于java - HashMap 困惑。读取/写入文件。 java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29265183/

相关文章:

file - 使用Delphi从文件中删除奇偶校验位(每9位)

HTML 文件 ://links : open in explorer from Chrome

java - 如果 Itunes :Keep iTunes Folder Organized is set programmatically on Windows,我该如何解决

java - 从数组中读取和检查值

arrays - 什么是最好的(实践)方法来存储有关单词在文本中的出现和位置的数据以便快速访问?

java - HashMap 对象键

java - 如何打印 HashMap 中的值而不打印重复项?

java - Java中套接字的有效使用

java - 从 Docker 连接到 localhost(使用 localstack 模拟 AWS 的 Java 应用程序)

java - 突然不受支持的 major.minor 版本 52.0