我認爲dedalo面臨的問題是他正在處理數組(我假設一個是鋸齒狀的,一個是多維的),並且他想將這些數據序列化爲blob。
但是,數組不是直接可序列化的,所以他問如何去做這件事。
去它會遍歷數組,並建立一個字符串作爲戴夫建議和存儲字符串的最簡單方法。正如duffymo指出的那樣,這將允許您從數據庫中的值中查看內容,而不是在需要時將數據反序列化。
如果您想知道如何將數組序列化爲BLOB ...(這似乎只是矯枉過正)
你能夠serialize one-dimensional arrays和jagged arrays,如:
public class Test {
public static void main(String[] args) throws Exception {
// Serialize an int[]
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("test.ser"));
out.writeObject(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
out.flush();
out.close();
// Deserialize the int[]
ObjectInputStream in = new ObjectInputStream(new FileInputStream("test.ser"));
int[] array = (int[]) in.readObject();
in.close();
// Print out contents of deserialized int[]
System.out.println("It is " + (array instanceof Serializable) + " that int[] implements Serializable");
System.out.print("Deserialized array: " + array[0]);
for (int i=1; i<array.length; i++) {
System.out.print(", " + array[i]);
}
System.out.println();
}
}
至於什麼數據類型來存儲它作爲MySQL中,there are only four blob types to choose from:
The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB
選擇滿足e最好的取決於序列化對象的大小。我想像BLOB
會很好。
我只需要存儲。此時此信息存儲在2個.txt文件中。當我需要閱讀這些文件時,我總是需要閱讀所有的值。我只是想知道如何使用MySQL而不是使用文本文件。感謝您的信息 – dedalo 2010-04-17 13:08:15