如何使用Buffered Reader只读取文本或只读取txt文件中的数字?

时间:2015-07-02 16:03:12

标签: java bufferedreader

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

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

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

我正在尝试使用Buffered reader返回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;
}

}

2 个答案:

答案 0 :(得分:0)

以下是使用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只有字母,如果它没有,那么你将不得不改变第二个正则表达式...

答案 1 :(得分:0)

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

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