Java 如何存储arraylist?

Java 如何存储arraylist?,java,mysql,xml,arraylist,Java,Mysql,Xml,Arraylist,我正在尝试在运行时之间存储我的ArrayList。因此,当我在ArrayList中输入项目时,关闭程序,然后重新打开它,并尝试搜索我放入的项目,它仍然会在那里 我尝试过MySQL和XML,但我无法理解。如果你能告诉我一种存储ArrayList的简单方法,那将是令人惊讶的 编辑: 我正在尝试序列化此ArrayList: static ArrayList<Product> Chart=new ArrayList<Product>(); 看看对象序列化,它听起来正是您想要的

我正在尝试在运行时之间存储我的
ArrayList
。因此,当我在
ArrayList
中输入项目时,关闭程序,然后重新打开它,并尝试搜索我放入的项目,它仍然会在那里

我尝试过MySQL和XML,但我无法理解。如果你能告诉我一种存储
ArrayList
的简单方法,那将是令人惊讶的

编辑:

我正在尝试序列化此
ArrayList

static ArrayList<Product> Chart=new ArrayList<Product>();

看看对象序列化,它听起来正是您想要的


这允许您将Java对象写入文件,并在以后将其读回。

查看对象序列化,它听起来正是您想要的


这允许您将Java对象写入文件,并在以后将其读回。

ObjectOutputStream和ObjectInputStream

ObjectOutputStream和ObjectInputStream

以下是一个示例。它还可以将文件拉上拉链,尽管您不必这样做。您还可以使用流来写入MYSQL blob,而不是文件

public static <T> void writeToGZIP(String filename, T obj) {
    ObjectOutputStream os = null;

    try {
        os = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(filename)));
        os.writeObject(obj);                    
        System.out.println("Object written to " + filename);
    } catch (Exception e) {         
        e.printStackTrace();            
    } finally {
        try {
            if (os != null) { os.flush(); os.close(); }
        } catch (IOException e) {           
            e.printStackTrace();
        }
    }       
}

@SuppressWarnings("unchecked")
public static <T> T readFromGZIP(String filename, T obj) {      
    ObjectInputStream ois = null;       

    try {
        ois = new ObjectInputStream(new GZIPInputStream(new FileInputStream(filename)));            
        obj = (T) ois.readObject();         
    } catch (Exception e) {         
        e.printStackTrace();            
    } finally { 
        try {
            if( ois != null ) ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return obj;
}
publicstaticvoidwritetogzip(字符串文件名,T obj){
ObjectOutputStream os=null;
试一试{
os=新的ObjectOutputStream(新的GziOutputStream(新的FileOutputStream(文件名));
os.writeObject(obj);
System.out.println(“写入”+文件名的对象);
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
如果(os!=null){os.flush();os.close();}
}捕获(IOE){
e、 printStackTrace();
}
}       
}
@抑制警告(“未选中”)
公共静态T readFromGZIP(字符串文件名,T obj){
ObjectInputStream ois=null;
试一试{
ois=新的ObjectInputStream(新的GZIPInputStream(新的FileInputStream(文件名));
obj=(T)ois.readObject();
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
如果(ois!=null)ois.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回obj;
}

以下是一个示例。它还可以将文件拉上拉链,尽管您不必这样做。您还可以使用流来写入MYSQL blob,而不是文件

public static <T> void writeToGZIP(String filename, T obj) {
    ObjectOutputStream os = null;

    try {
        os = new ObjectOutputStream(new GZIPOutputStream(new FileOutputStream(filename)));
        os.writeObject(obj);                    
        System.out.println("Object written to " + filename);
    } catch (Exception e) {         
        e.printStackTrace();            
    } finally {
        try {
            if (os != null) { os.flush(); os.close(); }
        } catch (IOException e) {           
            e.printStackTrace();
        }
    }       
}

@SuppressWarnings("unchecked")
public static <T> T readFromGZIP(String filename, T obj) {      
    ObjectInputStream ois = null;       

    try {
        ois = new ObjectInputStream(new GZIPInputStream(new FileInputStream(filename)));            
        obj = (T) ois.readObject();         
    } catch (Exception e) {         
        e.printStackTrace();            
    } finally { 
        try {
            if( ois != null ) ois.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return obj;
}
publicstaticvoidwritetogzip(字符串文件名,T obj){
ObjectOutputStream os=null;
试一试{
os=新的ObjectOutputStream(新的GziOutputStream(新的FileOutputStream(文件名));
os.writeObject(obj);
System.out.println(“写入”+文件名的对象);
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
如果(os!=null){os.flush();os.close();}
}捕获(IOE){
e、 printStackTrace();
}
}       
}
@抑制警告(“未选中”)
公共静态T readFromGZIP(字符串文件名,T obj){
ObjectInputStream ois=null;
试一试{
ois=新的ObjectInputStream(新的GZIPInputStream(新的FileInputStream(文件名));
obj=(T)ois.readObject();
}捕获(例外e){
e、 printStackTrace();
}最后{
试一试{
如果(ois!=null)ois.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回obj;
}


该线程的可能重复似乎更多地是关于基本数据库访问,而很少涉及序列化。我在这里提供了一对非常简单的方法。该线程的可能副本似乎更多地是关于基本数据库访问的,而很少涉及序列化。我在这里提供了一对非常简单的方法。请注意,要使其工作,内容需要可序列化。可序列化对象和不可序列化对象的示例是什么?
Thread
例如,不可序列化(无论如何,这没有多大意义)。最基本的是,对象中的所有引用也需要指向其他可序列化对象,最常见的数据类型是。请注意,要使其工作,内容需要可序列化。可序列化对象和不可序列化对象的示例是什么?例如,
Thread
不可序列化(无论如何,这没有多大意义)。最基本的是,对象中的所有引用也需要指向其他可序列化对象,最常见的数据类型是。arraylist支持java.io.Serializable接口?我只想序列化双精度和字符串。这些是可序列化的吗?是的,字符串和所有原语都是可序列化的。arraylist支持java.io.serializable接口?我只想序列化双精度和字符串。这些是序列化的吗?是的,字符串和所有原语都是可序列化的。你有没有gzip的示例?只需从上面删除
new GZIPOutputStream()
new gzipoutstream()
。你有没有gzip的示例?只需从上面删除
new gzipoutstream()
new gzipoutstream()