![](/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.