![](/img/trans.png)
[英]Java: When reading a text file, how can i read that specific line which contains a certain string?
[英]Is it possible to write a text file in such a way that when read by the Java compiler, it will add a line break at certain points?
对于我的 Java 课程,我正在处理一个本质上是 MTG 卡数据库的项目。 作为项目的一部分,我必须从文件中读取,因此我从文件中读取卡片信息,然后将行拆分以将每种不同类型的信息放在一起以形成不同类型卡片的不同对象类。 我现在遇到的主要挑剔问题是我需要将卡片文本放在文本文件的一行上,以便我可以一行一行地阅读它,但如果不是全部在一行上,我更愿意当我将它打印到控制台时。 有没有办法在文件本身的文本中添加一个字符组合,它会告诉我的编译器,“这里换行”,当它读到那个时,或者我运气不好? 我知道我可以在代码中使用 \\n 来实现这一点,但是当我遍历文件时,没有办法正确地执行我所知道的,因为并非每张卡片的文本都需要插入换行符。 如果重要的话,这是我处理该问题的代码块:
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;
public class MTG {
public static void main(String[] args) {
int creatureLength = 4;
//Prompt User
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Magic: the Gathering card database. This tool currently supports Rare and Mythic Rare cards from the Throne of Eldraine Expansion.");
try {
System.out.println("\nSelect the card type you'd like to view.");
System.out.println(""
+ "(1)Creatures\n"
);
int choice = Integer.parseInt(sc.next());
//Choose type
//Creatures
if(choice == 1){
Creature[] creatures = creatureGen("textfiles/Creatures.txt", creatureLength);
System.out.println("\nViewing creatures. Which card would you like to view?: \n");
for(int k = 0; k < creatureLength; k++) {
System.out.println(
"(" + (k + 1) + ") " + creatures[k].getName());
}
int creatureChoice = Integer.parseInt(sc.next());
try {
System.out.println("\n" + creatures[(creatureChoice - 1)]);}
catch(Exception e) {
System.out.println("Input was not a specified number. Exiting...");
}
}
}
catch(NumberFormatException ex){
System.out.println("Input was not a specified number. Exiting...");
}
sc.close();
}
//Read Creature text file
public static Creature[] creatureGen(String path, int length) {
Creature[] creatures = new Creature[length];
try {
FileReader file = new FileReader(path);
BufferedReader reader = new BufferedReader(file);
String name[] = new String[length];
String cost[] = new String[length];
String color[] = new String[length];
String type[] = new String[length];
String cTypes[] = new String[length];
String tags[] = new String[length];
String text[] = new String[length];
int power[] = new int[length];
int toughness[] = new int[length];
for (int i = 0; i < length; i++) {
String line = reader.readLine();
if(line != null) {
name[i] = line.split("\\|")[0];
cost[i] = line.split("\\|")[1];
color[i] = line.split("\\|")[2];
type[i] = line.split("\\|")[3];
cTypes[i] = line.split("\\|")[4];
tags[i] = line.split("\\|")[5];
text[i] = line.split("\\|")[6];
power[i] = Integer.parseInt(line.split("\\|")[7]);
toughness[i] = Integer.parseInt(line.split("\\|")[8]);
creatures[i] = new Creature(name[i], cost[i], color[i], type[i], cTypes[i], tags[i], text[i], power[i], toughness[i]);
}
}
reader.close();
}
catch (Exception e) {
System.out.println("Error reading file: " + path);
}
return creatures;
}
}
Creature 对象类本质上只是存储我使用 biogenGen 方法放入其中的数据。 我正在阅读的文本文件中的一个示例行如下所示:
Charming Prince|1W|White|Creature|Human Noble||When Charming Prince enters the battlefield, choose one — • Scry 2. • You gain 3 life. • Exile another target creature you own. Return it to the battlefield under your control at the beginning of the next end step.|2|2
例如,能够在这张卡片中的每个项目符号点之后插入换行符是理想的,但正如我之前所说,我需要将文本放在一行中以便循环阅读。 当我将它打印回控制台时,有什么办法可以解决这个问题吗? 我很感激任何帮助。
只需用换行符替换这些要点:
text[i] = line.split("\\|")[6].replaceAll("•","\n");
此外,您不应在每次需要元素时进行拆分,将 line.split("\\|") 的结果放入 String[] 变量中,然后再使用它。
for (int i = 0; i < length; i++) {
String line = reader.readLine();
if(line != null) {
String[] elements = line.split("\\|");
name[i] = elements[0];
cost[i] = elements[1];
color[i] = elements[2];
type[i] = elements3];
cTypes[i] = elements[4];
tags[i] = elements[5];
text[i] = elements[6].replaceAll("•","\n");
power[i] = Integer.parseInt(elements[7]);
toughness[i] = Integer.parseInt(elements[8]);
creatures[i] = new Creature(name[i], cost[i], color[i], type[i], cTypes[i], tags[i], text[i], power[i], toughness[i]);
}
}
最后,关于词汇,编译器不会读取您的文件。 编译器将您的代码转换为处理器的二进制指令(总结一下)。 您的文件在运行时被读取。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.