【问题标题】:How do I load a file with a bunch of descriptions for objects into an array?如何将包含一堆对象描述的文件加载到数组中?
【发布时间】:2016-04-25 14:24:04
【问题描述】:

我目前正在尝试编写我自己的魔法物品生成器供个人使用。现在我被困在尝试将一堆法术和对象描述加载到数组中,如果法术/对象被滚动到魔法物品的创建中,这些数组将从数组中调用。

我目前正试图将多个描述加载到一个数组中。在它退出之前,我似乎只能将一个描述放入数组中。

目前我有这个代码:

public class LoadDescription {
public static ArrayList<String> descriptions = new ArrayList<String>();

public static void main(String[] args) throws IOException {

    File filename = new File("spellconcept.txt");
    loadDescriptions(filename);

    System.out.println(descriptions.get(0));
    // System.out.println(descriptions.get(1));

}

public static void loadDescriptions(File name) throws IOException {
    FileReader fr = new FileReader(name);
    BufferedReader br = new BufferedReader(fr);
    StringBuilder sb = new StringBuilder();
    int i = 0;

    while (!br.readLine().equals("@@@")) {

        try {
            String line = br.readLine();
            while (!line.isEmpty()) {
                sb.append(" " + line);
                line = br.readLine();
            }
        } catch (IOException e) {
        }
        ;

        descriptions.add(sb.toString());
    }
    i++;
}}

这是我尝试使用的文本文件。请忽略它缺乏智能,它只是一个测试文件:

   @This is a description of a spell.
   I often wonder how often I can write
   the word often.Without seeming that it is too often that I write this out. 
   Does it not seem weird. The quick brown fox jumps over the small red fence.

    @This is another description of a spell


    @Maybe add another line here, \n see if this works? maybe?


    @@@

【问题讨论】:

    标签: java arrays file bufferedreader


    【解决方案1】:

    这是修改后的代码:

    示例文件:

    @这是一个咒语的描述。

    我经常想知道我可以多久写一次这个词。我写这个词似乎不太频繁。 是不是觉得很奇怪。敏捷的棕狐跳过红色的小栅栏。

    @这是对法术的另一种描述

    苹果是水果。 它是红色的。

    @3rd 描述

    我有一只宠物。 它是一只名叫约翰的狗。

    @@@

    public static ArrayList<String> descriptions = new ArrayList<String>();
    
        public static void main(String[] args) throws IOException {
    
            File filename = new File("C:\\temp\\test.txt");
            loadDescriptions(filename);
    
    
            System.out.println("*************");
        for(String ln:descriptions){
            System.out.println(ln);
        }
    
        }
    
        public static void loadDescriptions(File name) throws IOException {
            FileReader fr = new FileReader(name);
            BufferedReader br = new BufferedReader(fr);
            StringBuilder sb = new StringBuilder();
            int i = 0;
            String line=null;
    
            while ((line = br.readLine()) != null ) {
    
                if(line.startsWith("@")){
                    if(i>0){
                     descriptions.add(sb.toString());
                     sb = new StringBuilder();
                    }
    
                }else{
                   if(!line.isEmpty()){
                    System.out.println(line);
                    sb.append(" " + line);
                  }
                }
    
    
                i++;
    
            }
    
        }
    

    【讨论】:

    • System.out.println("*******************");在这种情况下怎么办?除此以外,其他一切都有意义。
    • 忽略它..它只是打印一行来标记数组打印的开始
    【解决方案2】:

    在循环的顶部,

       while (!br.readLine().equals("@@@")) {
    

    您调用 readLine(),使用 1-liner 行,但您没有捕获行本身来处理它。

    【讨论】:

    • 那你建议如何解决这个问题?我不知道我还能使用什么其他方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2023-01-15
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多