繁体   English   中英

如何使用缓冲读取器仅读取文本或txt文件中的数字?

[英]How do I use Buffered Reader to read only text, or only numbers in a txt file?

基本上我想做的就是阅读本文。

1591 : Dummy
1592 : Dummy
1593 : Dummy
1594 : Dummy
1595 : Dummy
1596 : Dummy

数字指的是NPC ID,文本指的是NPC名称。

我正在尝试使用缓冲读取器返回npc ID和npc名称。

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;

public class ReadFile {

private String path;

private int id;

public ReadFile(String file_path) {
    this.path = file_path;      
}

public String[] openFile() throws IOException {

    FileReader fr = new FileReader(path);
    BufferedReader textReader = new BufferedReader(fr);

    int numberOfLines = readLines();
    String[] textData = new String[numberOfLines];

    for (int i=0; i < numberOfLines; i++) {
        textData[i] = textReader.readLine();
    }

    textReader.close();
    return textData;        
}   


int readLines() throws IOException {

    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;
    int numberOfLines = 0;

    while ((aLine = bf.readLine()) != null) {
        numberOfLines++;
    }
    bf.close();
    return numberOfLines;
}

}

这是一个使用regex解析ID和名称的独立示例:

public static void main(String[] args) throws FileNotFoundException {
        String test = "1591 : Dummy 1592 : Dummy 1593 : Dummy 1594 : Dummy 1595 : Dummy 1596 : Dummy";
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher(test);
        while (m.find()) {
            System.out.println("Found a " + m.group() + ".");
        }


        p = Pattern.compile("[a-zA-Z]+");
        m = p.matcher(test);
        while (m.find()) {
            System.out.println("Found a " + m.group() + ".");
        }

 }

System.out

Found a 1591.
Found a 1592.
Found a 1593.
Found a 1594.
Found a 1595.
Found a 1596.
Found a Dummy.
Found a Dummy.
Found a Dummy.
Found a Dummy.
Found a Dummy.
Found a Dummy.

这个正则表达式假设name只有字母,如果没有,则必须更改第二个正则表达式...

这是一个完整的示例,包括如何返回所需的信息:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class NPCReader
{
    private final String filename;

    public NPCReader(String filename)
    {
        this.filename = filename;
    }

    List<NPC> getNPCs()
    {
        List<NPC> npcs = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
            String line;

            while ((line = reader.readLine()) != null) {
                String[] bits = line.split("/:/");

                if (bits.length != 2) {
                    continue;
                }

                npcs.add(new NPC(
                        Integer.parseInt(bits[0].trim(), 10),
                        bits[1].trim()));
            }
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
            return Collections.emptyList();
        }

        return npcs;
    }

    public static void main(String[] args)
    {
        NPCReader reader = new NPCReader("npcs.txt");

        List<NPC> npcs = reader.getNPCs();

        for (NPC npc : npcs) {
            System.out.println(npc);
        }
    }
}

class NPC
{
    private final int id;
    private final String name;

    public NPC(int id, String name)
    {
        if (name == null) {
            throw new NullPointerException("name cannot be null");
        }

        this.name = name;
        this.id = id;
    }

    public int id()
    {
        return id;
    }

    public String name()
    {
        return name;
    }

    @Override
    public String toString()
    {
        return String.format("My name is %s and my ID is %d", name, id);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM