gpt4 book ai didi

java - 从逗号分隔的 ArrayList 写入文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:03 27 4
gpt4 key购买 nike

我创建了一个 Song 类,其中包含歌曲的数据成员(标题、艺术家、专辑、长度)。我已经有一个 .txt 文件,其中包含存储在数组列表中的不同歌曲。用户完成添加或删除歌曲后,程序应以文本文件的原始格式(逗号分隔)写入文本文件。

我的问题是程序写入文件时没有使用逗号,当我通过获取 ArrayIndexOutOfBoundsException 重新运行它时,它会导致我的程序崩溃。我尝试使用嵌套 for 循环和 printf 函数,但没有成功。如何以其原始格式写入文件并避免在写入并尝试再次运行它后崩溃?

这是 .txt 文件的格式
岩龙虾,B-52,B-52,4:37
像埃及人一样行走,手镯,不同的光线,3:24

这是我的带有 toString() 方法的歌曲类

public class Song {
//Declaring all data members.
private String title;
private String artist;
private String album;
private String length;
private static int songCounter = 0;

//Constructors for Song class.
public Song(String title, String artist, String album, String length){
this.title = title;
this.artist = artist;
this.album = album;
this.length = length;
songCounter++;
}
//Get and Set methods
public String getTitle(){
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getArtist(){
return artist;
}

public void setArtist(String artist) {
this.artist = artist;
}

public String getAlbum(){
return album;
}

public void setAlbum(String album){
this.album = album;
}

public String getLength(){
return length;
}

public void setLength(String length){
this.length = length;
}

public static int getSongCounter(){
return songCounter;
}
public int compareArtist(Song o){
return artist.compareTo(o.artist);
}
public int compareTitle(Song o){
return title.compareTo(o.title);
}
//Overriding the toString() function.
@Override
public String toString(){
return title +","+artist+","+album+","+length;
}
}

这是我的主类,它从文件中读取并包含写入方法

public class Library {

public static void main(String[] args) {
ArrayList <Song> songList = new ArrayList <Song> ();
boolean repeat = true;

try{
Scanner read = new Scanner (new File("SongList.txt"));
do{
String line = read.nextLine();
String [] tokens = line.split(",");
songList.add(new Song(tokens[0], tokens[1], tokens[2], tokens[3]));
}while(read.hasNext());
read.close();
}catch (FileNotFoundException e){
System.out.println("File not found.");
}

while ( repeat ){
System.out.println("\nSelect a Function");
System.out.println("1. Search Song");
System.out.println("2. Add Song");
System.out.println("3. Delete Song");
System.out.println("4. Display Songs");
System.out.println("5. Quit");

switch (MenuInputCheck(1, 5)){

case 1: searchSong(songList);
break;
case 2: addSong(songList);
break;
case 3: deleteSong(songList);
break;
case 4: displaySorted(songList);
break;
case 5: saveFile(songList);
repeat = false;
break;
}
}
}

public static void saveFile(ArrayList <Song> songList){
try{
PrintWriter writer = new PrintWriter("SongList.txt");
for (int i = 0; i < songList.size();i++){
writer.println(i);
}
writer.close();
} catch (FileNotFoundException e){
System.out.println("File not found.");
}
}

最佳答案

目前代码只是编写一个整数

 for (int i = 0; i < songList.size();i++){
writer.println(i);
}

我建议您在 Song 中创建一个方法,该方法将写入以逗号分隔的字段

例如

public String writeMe () {
StringBuffer buf = new StringBuffer ();
buf.append (name).append(",").append(artist).....;

return buf.toString ();

}

然后在循环中调用

writer.write (singList.get(i).writeMe ();

更新

使用更新后的代码,您可以使用以下命令获取输出

writer.println(song.toString());

关于java - 从逗号分隔的 ArrayList 写入文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28757805/

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