1

I would like to use this method to convert a 2D Array to string into a properties file. Then, import it from the properties by converting the properties string back to a 2D Array. But I have difficulties with converting the String back to a 2D array.

EDIT: I want to reverse the 2D array string output back to 2D array. But the question you gave me doesn't help me with the 2D array one. As the Arrays.replace() can't replace the 2D array's internal one.

Here's my code:

public static void getSettings(){
try {
File file = new File("config.properties");
Properties p = new Properties();
FileInputStream fileInput = new FileInputStream(file);
//Import
moduleNames = p.getProperty("moduleNames").split("");
moduleData = ??? //I don't know how to convert String to String[][]
}
catch (Exception e){
e.printStackTrace();
}
}

Here's the another function to set property:

public static void writeFile(String[][] mD, String[] mN){
try{
Properties p = new Properties;
File file = new File("config.properties");

p.setProperty("moduleData", Arrays.deepToString("mD"));
p.setProperty("moduleNames", Arrays.toString("mN"));
//...further more code to flush the data out
} catch (Exception e){
e.printStackTrace();
}

}

Can anyone tell me how to convert String (from deepToString) back to String[][]?

My project is in Open source. This: Conf.java Line271 is the set properties one.

And this: Conf.java Line215 is the load String to String[][] one.

Community
  • 1
  • 1
  • 2
    Check http://stackoverflow.com/questions/456367/reverse-parse-the-output-of-arrays-tostringint – Tom Oct 16 '15 at 09:34
  • @Tom You can vote for duplicate. – YoungHobbit Oct 16 '15 at 09:38
  • 2
    There is no method in the java API that will automatically convert this back to an array that i know of. someone already did the thing you need : [stringToDeep](http://stackoverflow.com/a/22428926/4088809) @YoungHobbit :) – Tahar Bakir Oct 16 '15 at 09:40
  • No, I am asking for multidimensional array. But thank you Tom and Tahar Bakir for the URL, it also helps me a lot. –  Oct 16 '15 at 10:01

1 Answers1

0

I could not find any standard method that could do that. But using the ideas from here I have formulated the following:

        String[][] sample = new String[][]{{"Apple", "Ball"},{"Cat", "Dog"}, {"Elephant, Fish"} };
        String temp = Arrays.deepToString(sample);
        String[] strings = temp.replace("[", "").replace("]", ">").split(", ");
        List<String> stringList = new ArrayList<>();
        List<String[]> tempResult = new ArrayList<>();
        for(String str : strings) {
            if(str.endsWith(">")) {
               str = str.substring(0, str.length() - 1);
               if(str.endsWith(">")) {
                   str = str.substring(0, str.length() - 1);
               }
               stringList.add(str);
               tempResult.add(stringList.toArray(new String[stringList.size()]));
                stringList = new ArrayList<>();
            } else {
                stringList.add(str);
            }
        }
       String[][] originalArray = tempResult.toArray(new String[tempResult.size()][]);

This has lots of scope for improvement and I hope you will do that. This is just for hint purpose for converting the String back to a 2D array.

Community
  • 1
  • 1
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
  • Thank you! Your code helps me. But Tahar Bakir's comment has solved my problem. –  Oct 16 '15 at 10:00