Sayfalar

24 Mayıs 2011 Salı

Save and Load in JAVA

Here are the codes for saving and loading an arraylist to/from file. You can design it to an array or something else.


Java'da arraylist'i dosyaya kaydetme ve kayıtlı dosyadan arraylist'i programa yüklemek için gerekli olan kod. Arraylist yerine başka bir türe çevirebilirsiniz.(dizi gibi)




public static void savingArrayList(File saveFile , ArrayList<MyClass> myArrayList ){try {FileOutputStream fos = new FileOutputStream(saveFile);GZIPOutputStream gzos = new GZIPOutputStream(fos);ObjectOutputStream out = new ObjectOutputStream(gzos);out.writeObject(myArrayList);out.flush();out.close();}catch (IOException e) {System.out.println(e);}}public static void loadingArrayList(File selectedFile){try {FileInputStream fis = new FileInputStream(selectedFile);GZIPInputStream gzis = new GZIPInputStream(fis);ObjectInputStream in = new ObjectInputStream(gzis);ArrayList<MyClass> myNewArrayList = (ArrayList<MyClass>)in.readObject();in.close();}catch (Exception e) {System.out.println(e);}}

Hiç yorum yok:

Yorum Gönder