解析信息后动态输出数组信息

时间:2014-06-06 20:53:15

标签: java arrays arraylist

我正在尝试解析一些已定义的字体样式,以创建一个数组,用于确定要在文档中使用的字体样式。目前,我只是显示存储在数组中的信息,看看我是否正确,但我注意到它是静态存储信息,这不是我想要的。

这是我正在解析的信息:

<!Font Definitions,
    F2 = Times 6,
    F3 = Times 10,
    F7 = Times 10 Bold,
    F8 = Times 9,
    F6 = Times 12,
    F9 = Symbols 12,
    F12 = Times 10 Italic,
    F13 = Helvetica 14 Bold,
    F15 = Times 12 Bold,
    F16 = Times 15,
    F17 = Times 14 Bold,
    F18 = Times 18 Bold,
    F25 = Times 6 Bold,
    F26 = Times 6 Italic,
    F27 = Times 21 Bold,
    F28 = Times 27 Bold,
    F30 = Times 15 Bold,
    F37 = Times 9 Bold,
    F38 = Times 9 Italic>

我尝试了两次不同的时间来获得我想要的输出。这是第一次尝试:

*注意:text.jor.ildoc文件是QuickSilver文件。如果您将其放入文本文档中,我正在解析的信息也是一样的。

public class Test {

    private class FontF {
        private String FontID;
        private String FontFamily;
        private String FontSize;
        private String FontAttribute;
    }

    Test test = new Test();
    FontF fontF = test.new FontF();

    public static void main(String[] args) {
        File infile = new File("C:\\Users\\Jake\\Sandbox\\test.jor.ildoc");
                ArrayList<FontF> fontStuff = new ArrayList<FontF>();
        try {
            FileReader in = new FileReader(infile);
            BufferedReader readFile = new BufferedReader(in);
            String line;
            while (!((line = readFIle.readLine()).contains("<!Font Definitions,"))) {

        }
        int counter = 0;
        while (!((line = readFile.readLine()).compareTo("") == 0)) {
            String id,temp;
            id = line.substring(0, line.indexOf(" "));
            id = id.trim();
            temp = line.substring(line.indexOf("=") + 1);
            temp = temp.trim();
            temp = temp.substring(0, temp.length() - 1);
            String[] array = temp.split(" ");
            fontStuff.add(fontF);
            fontStuff.get(counter).FontID = id;
            fontStuff.get(counter).FontFamily = array[0];
            fontStuff.get(counter).FontSize = array[1];
            if (array.length == 3) {
                fontStuff.get(counter).FontAttribute = array[2];
            } else {
                fontStuff.get(counter).FontAttribute = "Normal";
            }
            counter++;
        }
    }
    for (int i = 0; i < fontStuff.size(); i++) {
        System.out.println("FontID: " + fontStuff.get(i).FontAttribute);
        System.out.println("FontFamily: " + fontStuff.get(i).FontFamily);
        System.out.println("FontSize: " + fontStuff.get(i).FontSize);
        System.out.println("FontAttribute: " + fontStuff.get(i).FontAttribute);
        System.out.println();
    }
    readFile.close();
    in.close();
} catch (Exception e) {
    e.printStackTrace();
}
}

这是第二次:

public class Test {

public static void main(String[] args) {
    String FontID = "";
    String FontFamily = "";
    String FontSize = "";
    String FontAttribute = "";
    ArrayList<String> fontVariables = new ArrayList<String>();
    fontVariables.add(FontID);
    fontVariables.add(FontFamily);
    fontVariables.add(FontSize);
    fontVariables.add(FontAttribute);
    ArrayList<ArrayList<String>> fontStuff = new ArrayList<ArrayList<String>>();
    File infile = new File("C:\\Users\\Jake\\Sandbox\\test.jor.ildoc");
    try {
        FileReader in = new FileReader(infile);
        BufferedReader readFile = new BufferedReader(in);
        String line;
        while (!((line = readFile.readLine()).contains("<!Font Definitions,"))) {

        }
        int counter = 0;
        while (!((line = readFile.readLine()).compareTo("") == 0)) {
            if (line.contains("=")) {
                String id,temp;
                id = line.substring(0,line.indexOf(" "));
                id = id.trim();
                temp = line.substring(line.indexOf("=") + 1);
                temp = temp.trim();
                temp = temp.substring(0, temp.length() - 1);
                String[] array = temp.split(" ");
                fontStuff.add(fontVariables);
                fontStuff.get(counter).set(0, id);
                fontStuff.get(counter).set(1, array[0]);
                fontStuff.get(counter).set(2, array[1]);
                if (array.length==3) {
                    fontStuff.get(counter).set(3, array[2]);
                } else {
                    fontStuff.get(counter).set(3, "Normal");
                }
                counter++;
            }
        }
        for (int i = 0; i < fontStuff.size(); i++) {
            System.out.println("FontID: " + fontStuff.get(i).get(0));
            System.out.println("FontFamily: " + fontStuff.get(i).get(1));
            System.out.println("FontSize: " + fontStuff.get(i).get(2));
            System.out.println("FontAttribute: " + fontStuff.get(i).get(3));
            System.out.println();
        }
        readFile.close();
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

如果有人可以帮助我正确显示信息,我真的很感激输入。目前,它仅停留显示最后一次字体信息19次(F38)。

1 个答案:

答案 0 :(得分:1)

您要将相同的变量(fontVariables)添加到fontStuff列表中。但是Java只添加对列表的引用,因此您可以获得多次引用同一变量的列表。这就是你最后一次读取字体的原因。

要保留所有字体的列表,必须在循环的每次迭代中分配一个新的fontVariables