如何使用Java解析文本文件以将某些行写入另一个文本文件?

时间:2018-08-18 23:47:16

标签: java java.util.scanner

我正在学习如何使用Java处理文件。我有一个示例文件,其中包含密钥对及其值。我试图找到一个密钥对,如果匹配,那么输出文件将同时更新,密钥对及其值。我能够在输出文件中获取密钥对,但也无法获取值。 Stringbuilder可能在这里工作以追加字符串,但我不知道如何。

下面是我的输入和输出文件。

Input File:

born time 9 AM London -- kingNumber 1234567890 -- address: abc/cd/ef -- birthmonth: unknown
born time 9 AM Europe -- kingNumber 1234567890 -- address: abc/cd/ef -- birthmonth: december

Expected Output File:

kingNumber 1234567890 birthmonth unknown 
kingNumber 1234567890 birthmonth unkbown

Current Output File:

kingNumber birthmonth 
kingNumber birthmonth

我能够写密钥对(在这种情况下为“ kingNumber”和“ birthmonth”)来输出文件,但是我不确定该怎么做才能得到它的值。

    String kn = "kingNumber:";
    String bd = "birthmonth:";

    try {

        File f = new File("sample.txt");
        Scanner sc = new Scanner(f);
        FileWriter fw = new FileWriter("output.txt");


        while(sc.hasNextLine()) {
            String lineContains = sc.next();
            if(lineContains.contains(kn)) {
                fw.write(kn  + "\n");
                // This is where I am stuck. What
                // can I do to get it's value (number in this case).
            }
            else if(lineContains.contains(bd)) {
                fw.write(bd);
                // This is where I am stuck. What
                // can I do to get it's value (birthday in this case).
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:0)

我编写了一个简单的解析器,它遵循您示例中的数据格式。 您将需要这样称呼它:

PairParser parser = new PairParser(lineContains);

然后您可以通过配对键从解析器获取价值 如何获得价值:

parser.getValue("kingNumber")

请注意,键没有尾随列字符。

解析器代码在这里:

package com.grenader.example;

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

public class PairParser {

    private Map<String, String> data = new HashMap<>();

    /**
     * Constructor, prepare the data
     * @param dataString line from the given data file
     */
    public PairParser(String dataString) {
        if (dataString == null || dataString.isEmpty())
            throw new IllegalArgumentException("Data line cannot be empty");

        // Spit the input line into array of string blocks based on '--' as a separator
        String[] blocks = dataString.split("--");

        for (String block : blocks)
        {
            if (block.startsWith("born time")) // skip this one because it doesn't looks like a key/value pair
                continue;
            String[] strings = block.split("\\s");
            if (strings.length != 3) // has not exactly 3 items (first items is empty), skipping this one as well
                continue;
            String key = strings[1];
            String value = strings[2];
            if (key.endsWith(":"))
                key = key.substring(0, key.length()-1).trim();

            data.put(key.trim(), value.trim());
        }
    }

    /**
     * Return value based on key
     * @param key
     * @return
     */
    public String getValue(String key)
    {
        return data.get(key);
    }

    /**
     * Return number of key/value pairs
     * @return
     */
    public int size()
    {
        return data.size();
    }
}

这是确保代码正常工作的单元测试

package com.grenader.example;

import com.grenader.example.PairParser;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class PairParserTest {

    @Test
    public void getValue_Ok() {
        PairParser parser = new PairParser("born time 9 AM London -- kingNumber 1234567890 -- address: abc/cd/ef -- birthmonth: unknown");

        assertEquals("1234567890", parser.getValue("kingNumber"));
        assertEquals("unknown", parser.getValue("birthmonth"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void getValue_Null() {
        new PairParser(null);
        fail("This test should fail with Exception");
    }

    @Test(expected = IllegalArgumentException.class)
    public void getValue_EmptyLine() {
        new PairParser("");
        fail("This test should fail with Exception");
    }

    @Test()
    public void getValue_BadData() {
        PairParser parser = new PairParser("bad data bad data");
        assertEquals(0, parser.size());

    }
}

答案 1 :(得分:0)

您可以将java.util.regex.Patternjava.util.regex.Matcher用于类似的模式:

^born\stime\s([a-zA-Z0-9\s]*)\s--\skingNumber\s(\d+)\s--\saddress:\s([a-zA-Z0-9\s/]*)\s--\sbirthmonth:\s([a-zA-Z0-9\s]*)$

少写,多做。