You must try with below code:
try(FileInputStream fis = new FileInputStream(fileName)){
XMLDecoder decoder = new XMLDecoder(fis);
Object[][] data = (Object[][]) decoder.readObject();
decoder.close();
return data;
}
Entire code:
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException {
String fileName = "data.xml";
Object[][] data = new Object[][]{
new String[] {"youtube", "max", "mail.com", "password"},
new String[] {"youtube", "max", "mail.com", "password"},
new String[] {"youtube", "max", "mail.com", "password"},
new String[] {"youtube", "max", "mail.com", "password"}
};
serializeToXML(data,fileName);
Object[][] xmlData = deserializeFromXML(fileName);
for (Object[] xmlDatum : xmlData) {
for (Object xmlDataun : xmlDatum) {
System.out.print(xmlDataun.toString()+" ");
}
System.out.println();
}
}
private static void serializeToXML (Object[][] data,String fileName) throws IOException
{
try(FileOutputStream fos = new FileOutputStream(fileName)){
try(XMLEncoder encoder = new XMLEncoder(fos)){
encoder.writeObject(data);
}
}
}
private static Object[][] deserializeFromXML(String fileName) throws IOException {
try(FileInputStream fis = new FileInputStream(fileName)){
XMLDecoder decoder = new XMLDecoder(fis);
Object[][] data = (Object[][]) decoder.readObject();
decoder.close();
return data;
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…