首页 > 解决方案 > 是否有可能以这样的方式编写文本文件,当 Java 编译器读取时,它会在某些点添加换行符?

问题描述

对于我的 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 对象类本质上只是存储了我使用 creativeGen 方法放入其中的数据。我正在读取的文本文件中的示例行如下所示:

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

例如,能够在这张卡片的每个要点之后插入换行符是理想的,但正如我之前所说,我需要将文本放在一行中,以便循环读取它。当我将它打印回控制台时,有什么办法可以解决这个问题吗?我很感激任何帮助。

标签: javafileprintingformatbufferedreader

解决方案


只需用换行符替换那些要点:

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

最后,关于词汇,编译器不会读取您的文件。编译器将您的代码转换为处理器的二进制指令(总结)。您的文件在运行时被读取。


推荐阅读