gpt4 book ai didi

java - 将 ArrayList 保存到文本文件

转载 作者:行者123 更新时间:2023-12-03 18:44:28 25 4
gpt4 key购买 nike

我一直在尝试获取一个 ArrayList 以保存到一个文件中。我可以看到它正在创建文本文件,但文本文件中没有任何内容,只是空白。

这是ArrayList的主要代码,带有保存选项的开关。

static int input, selection, i = 1;
static ArrayList<Animals> a;

// Main Method
public static void main(String[] args){

// Create an ArrayList that holds different animals
a = new ArrayList<>();
a.add(new Animals(i++, "Bear", "Vertebrate", "Mammal"));
a.add(new Animals(i++, "Snake", "Invertebrate", "Reptile"));
a.add(new Animals(i++, "Dog", "Vertebrate", "Mammal"));
a.add(new Animals(i++, "Starfish", "Invertebrates", "Fish"));

while (true) {
try {
System.out.println("\nWhat would you like to do?");
System.out.println("1: View List\n2: Delete Item\n3: Add Item\n4: Edit Item\n5: Save File\n0: Exit");
selection = scanner.nextInt();
if(selection != 0){
switch (selection) {
case 1:
ViewList.view();
Thread.sleep(4000);
break;
case 2:
Delete.deleteItem();
Thread.sleep(4000);
break;
case 3:
Add.addItem();
Thread.sleep(4000);
break;
case 4:
Edit.editItem();
Thread.sleep(4000);
break;
case 5:
Save.saveToFile("animals.txt", a);
Thread.sleep(4000);
break;

这是我写的处理文件。

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

public class Save extends ALProgram{
public static void saveToFile(String fileName, ArrayList list){
Path filePath = Paths.get(fileName);
try{
System.out.println("File Saved");
Files.write(filePath, list, Charset.defaultCharset());
}catch(IOException e){
e.printStackTrace();
}

}
}

这是动物类

class Animals {

public int id;
public String type, vertebrate, aclass;

public Animals(int id, String type, String vertebrate, String aclass) {
this.id = id;
this.type = type;
this.vertebrate = vertebrate;
this.aclass = aclass;

}

public int getID() {
return id;
}

public String getType() {
return type;
}

public String getVert() {
return vertebrate;
}

public String getaclass() {
return aclass;
}

}

最佳答案

有两个变化:

  1. 您的类需要实现 CharSequence 才有资格传递给 Files.write。
  2. 您需要覆盖 toString 方法以指定您的内容在保存时的外观。经过以上两个更改后,我可以看到输出。

        class Animals implements CharSequence {

    public int id;
    public String type, vertebrate, aclass;

    public Animals(int id,String type,String vertebrate,String aclass) {
    this.id = id;
    this.type = type;
    this.vertebrate = vertebrate;
    this.aclass = aclass;
    }

    public int getID() {
    return id;
    }

    public String getType() {
    return type;
    }

    public String getVert() {
    return vertebrate;
    }

    public String getaclass() {
    return aclass;
    }

    @Override
    public int length() {
    return toString().length();
    }

    @Override
    public char charAt(int index) {
    return toString().charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
    return toString().subSequence(start, end);
    }

    /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
    @Override
    public String toString() {
    return "Animals [id=" + id + ", type=" + type + ", vertebrate=" + vertebrate + ", aclass=" + aclass + "]";
    }


    }

关于java - 将 ArrayList 保存到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38045315/

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