0

I made a FileOutputStream, which saves an arraylist full of String[]'s to an xml file. The problem is, that when I load the file, I can only load it as a String. The Strings are some weird codes likes:

[Ljava.lang.String;@75a7d0f4

I need help to, save (or load) the String[]'s so I can call them probably

And, in the load method, I need to pass all the data to the addSong() method with a loop?

Code:

public class Songs {
List<String[]> songs = new ArrayList<String[]>();

public void addSong(String s, String a, String yt){
    String[] songarray= new String[3];
    songarray[0] = s;
    songarray[1] = a;
    songarray[2] = yt;
    songs.add(songarray);


}
public void editSong(int i, String s, String a, String yt){
    String[] editsongarray = new String[3];
    editsongarray[0] = s;
    editsongarray[1] = a;
    editsongarray[2] = yt;
    songs.remove(i);
    songs.add(i,editsongarray);
}
public void removeSong(int i){
    songs.remove(i);
}

public String[] getList(int i){
    String[] j = songs.get(i);
    return j;
}
public void save() throws FileNotFoundException{
    PrintWriter pw = new PrintWriter(new FileOutputStream("C:\\Applications\\songs.xml"));
    for(String[] x:songs)
        pw.println(x);
        pw.close();
}
public void load()throws FileNotFoundException{
    FileInputStream in = new FileInputStream("C:\\Applications\\songs.xml");
    Scanner scan = new Scanner(in);
    String x = scan.next();
    //String[] x = scan.next(); doesnt works 

    System.out.println(x);

}

}

2 Answers 2

2

That's the default output when you print an array. If you want to see the actual content use Arrays.toString()

Sign up to request clarification or add additional context in comments.

2 Comments

Could be me misunderstanding something. But in my load method, i load the xml file as Strings.. The problem is pretty much i need to convert the String to an array(String[]) , or load the xml file as an array(String[]). Array.toString(), wouldt work on a String, right?? Thanks in advantage, i appreciate it :-)
If you want to parse xml use something like dom4j (easiest) or stax (better for really large xml documents but more complicated)
1

You can not write the contents in to xml file as just plain strings. You need to follow the xml standards like

<songs>
   <song>song1</song>
   <song>song2</song>
<songs>

Also while reading you need to parse these nodes to extract the songs from the xml. There are quite a few API's to deal with xmls. You need to use them.

Jdom is one of the easy to use parsers. Here is the simple example. You don't need to use the maven and all, you can download the jar from jdom website.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.