如何使用 Buffered Reader 来只读取文本,或者只读取 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名称
我正在尝试使用 Buffered reader 来 return 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
中只有字母,如果没有,则您将不得不更改第二个正则表达式...
这里有一个完整的例子,包括如何return你想要的信息:
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);
}
}
基本上我想做的就是阅读这篇文章。
1591 : Dummy 1592 : Dummy 1593 : Dummy 1594 : Dummy 1595 : Dummy 1596 : Dummy
数字为NPC id,文字为NPC名称
我正在尝试使用 Buffered reader 来 return 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
中只有字母,如果没有,则您将不得不更改第二个正则表达式...
这里有一个完整的例子,包括如何return你想要的信息:
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);
}
}