我試圖解析一些定義的字體樣式來創建一個數組,用於確定在文檔中使用什麼樣的字體樣式。目前,我只是顯示存儲在數組中的信息,以查看是否正確,但我注意到它正在靜態存儲信息,這不是我想要的。解析信息後動態輸出數組信息
這是我解析通過信息:我試過兩個不同的時間來獲得我想要的輸出
<!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)。
爲什麼不使用'String'類的'split'函數從開始分割並使用它來訪問每個組件。然後,而不是使用'ArrayList',將數據存儲在'HashMap'中,其中您的鍵是字體ID,值是包含字體信息的數組列表。 –
我必須嘗試。我意識到這是不是格式良好的代碼(對不起),我只是要編碼它的工作,然後清理它。以前從未使用過'HashMap',我不得不看看那些。 – Jake
您能否告訴我們fontStuff來自哪裏?執行該操作可能存在問題。 –