从文本文件中获取字符串并将每行指定为值(一次2个并插入LinkedHashMap)

时间:2014-01-05 09:09:04

标签: java file-io map bufferedreader

我要做的是,加载一个Text文件,然后从每一行获取值并将它们分配给我的程序中的变量。每两行,我会将它们插入LinkedHashMap(作为一对)

缓冲读卡器的问题是,我似乎只是一次读取一行。

这是我目前的代码:

public static void receiver(String firstArg) {// Receives
                                                // Input
                                                // File
    String cipherText;
    String key;
    String inFile = new File(firstArg).getAbsolutePath();
    Path file = new File(inFile).toPath();

    // File in = new File(inFile);
    try (InputStream in = Files.newInputStream(file);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in))) {
        String line = null;

        while ((line = reader.readLine()) != null) {
            // System.out.println(line);
            String[] arrayLine = line.split("\n"); // here you are
                                                    // splitting
                                                    // with whitespace

            cipherText = arrayLine[0];
            // key = arrayLine[1];
            System.out.println(arrayLine[0] + " " + arrayLine[1]);

            cipherKeyPairs.put(arrayLine[0], arrayLine[1]);
        }
    } catch (IOException x) {
        System.err.println(x);
    }

问题是,它找不到arrayLine[1](原因很明显)。我需要它一次读取两行而不使数组超出范围。

任何想法如何做到这一点,以便我可以将它们存储到我的LinkedHashMap中,一次两行作为单独的值。

2 个答案:

答案 0 :(得分:3)

您可以通过在列表中每2行读取一次来解决此问题 该代码的描述是:“Bold是真实的案例”

  1. 读取第一行(计数为0)
    • 如果(secondLine为false)==>将该行保存到CipherText变量,使secondLine = true
    • 否则如果(secondLine为真)==>添加到列表(CipherText,行),使secondLine = false
  2. 阅读第二行(计数为1)
    • 如果(secondLine为假)==>将该行保存到CipherText变量,使secondLine = true
    • 否则如果(secondLine为true)==>添加到列表(CipherText,行),使secondLine = false

  3. String cipherText;
    boolean secondLine = false;
    String inFile = new File(firstArg).getAbsolutePath();
    Path file = new File(inFile).toPath();
    
    try {
        InputStream in = Files.newInputStream(file);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
    
        String line = null;
    
        while ((line = reader.readLine()) != null) {
    
    
            if (!secondLine) //first line reading
            {
                cipherText = line; 
                secondLine = true;
            }
            else if (secondLine) //second line reading
            {   
                cipherKeyPairs.put(cipherText, line);
                secondLine = false;
            }
        }
    } catch (IOException x) {
        System.err.println(x);
    }
    

答案 1 :(得分:1)

看看这是否适合您。我刚编辑了你的代码。这可能不是最好的答案。

public static void receiver(String firstArg) {// Receives
                                            // Input
                                            // File
    String cipherText;
    String key;
    String inFile = new File(firstArg).getAbsolutePath();
    Path file = new File(inFile).toPath();

// File in = new File(inFile);
    try (InputStream in = Files.newInputStream(file);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(in))) {
        String line = null;
        List<String> lines = new ArrayList();
        while ((line = reader.readLine()) != null) {

           lines.add(line);//trim line first though and check for empty string
        }

        for(int i=1;i<lines.size();i++){
             cipherText = arrayLine[i];
            // key = arrayLine[1];
            System.out.println(arrayLine[i] + " " + arrayLine[i-1]);

            cipherKeyPairs.put(arrayLine[i-1], arrayLine[i]);
        }
    } catch (IOException x) {
        System.err.println(x);
    }
}