Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
99 views
in Technique[技术] by (71.8m points)

java - Load two dimensional array from xml file

So I hava a a two dimensional array of objects which I can store into an XML file using encoder.writeObject. Now I want to load the stored Array back into a new array.

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"}
        };
encoder.writeObject(data);

now i want to read the file with this method:

data = decoder.readObject();
question from:https://stackoverflow.com/questions/65880381/load-two-dimensional-array-from-xml-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        }

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...