I am trying to store my ArrayList
between runtimes. So when I enter items into the ArrayList
then close the program down and then open it back up and try to search the item I put in it will still be there.
我试图在运行时存储我的ArrayList。所以当我在ArrayList中输入项目时然后关闭程序然后打开它然后尝试搜索我放入的项目它仍然在那里。
I have tried MySQL and XML but I cannot figure it out. If you could please direct me in the direction of an easy way to store ArrayList
that would be amazing.
我尝试过MySQL和XML,但是我搞不清楚。如果你能告诉我一个简单的储存ArrayList的方法,那就太棒了。
Edit:
编辑:
I am trying to serialize this ArrayList
:
我正在尝试序列化这个ArrayList:
static ArrayList<Product> Chart=new ArrayList<Product>();
with these objects:
与这些对象:
double Total;
String name;
double quantity;
String unit;
double ProductPrice;
3 个解决方案
#1
4
Look into object serialization, it sounds like exactly what you want
查看对象序列化,它听起来正是您想要的
对象串行化是什么?
This allows you to write a Java object to a file and read it back in later.
这允许您将Java对象写入文件并在稍后读取它。
#2
1
Here is an example. It also zips up the file, although you don't have to do that. You can also use the stream to write to a MYSQL blob instead of a file.
这是一个例子。它还压缩文件,尽管您不必这样做。您还可以使用流来写入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;
}
#3
0
ObjectOutputStream and ObjectInputStream
ObjectOutputStream和ObjectInputStream
#1
4
Look into object serialization, it sounds like exactly what you want
查看对象序列化,它听起来正是您想要的
对象串行化是什么?
This allows you to write a Java object to a file and read it back in later.
这允许您将Java对象写入文件并在稍后读取它。
#2
1
Here is an example. It also zips up the file, although you don't have to do that. You can also use the stream to write to a MYSQL blob instead of a file.
这是一个例子。它还压缩文件,尽管您不必这样做。您还可以使用流来写入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;
}
#3
0
ObjectOutputStream and ObjectInputStream
ObjectOutputStream和ObjectInputStream