gpt4 book ai didi

Java读取xml文件时出错

转载 作者:行者123 更新时间:2023-12-01 23:14:56 26 4
gpt4 key购买 nike

我创建了一个 FileOutputStream,它将一个充满 String[]arraylist 保存到一个 xml 文件中。问题是,当我加载文件时,我只能将其作为字符串加载。字符串是一些奇怪的代码,例如:

[Ljava.lang.String;@75a7d0f4

我需要帮助来保存(或加载)String[],以便我可以调用它们

并且,在 load 方法中,我需要使用循环将所有数据传递给 addSong() 方法?

代码:

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

}

}

最佳答案

这是打印数组时的默认输出。如果您想查看实际内容,请使用 Arrays.toString()

关于Java读取xml文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21391821/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com