Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaapplication1;
- import java.io.*;
- import java.util.*;
- import java.text.*;
- public class Main {
- public static Set<obiekt> zbior = new LinkedHashSet<obiekt>();
- public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
- public static void main(String[] args) throws Exception
- {
- File dir = new File("C:\\xampp\\tmp");
- save(dir, "");
- readto("test.ser");
- readfrom("test.ser");
- }
- private static void save(File dir, String path)
- {
- File[] dzieci = dir.listFiles();
- if(dzieci != null)
- {
- for(int i=0; i< dzieci.length; ++i)
- {
- String str = path + dzieci[i].getName();
- Date d = new Date(dzieci[i].lastModified());
- if(dzieci[i].isDirectory() == true)
- {
- obiekt dziecko = new obiekt(str,sdf.format(d),dzieci[i].list().length, "folder");
- zbior.add(dziecko);
- save(dzieci[i], path + ">>");
- }
- else
- {
- obiekt dziecko = new obiekt(str,sdf.format(d),dzieci[i].length(), "plik");
- zbior.add(dziecko);
- }
- }
- }
- }
- private static void readfrom(String path) throws Exception
- {
- FileInputStream fis = new FileInputStream(path);
- ObjectInputStream ois = new ObjectInputStream(fis);
- Set<obiekt> b = (LinkedHashSet<obiekt>) ois.readObject();
- Iterator it=b.iterator();
- while(it.hasNext())
- {
- obiekt value =(obiekt)it.next();
- System.out.println(value.nazwa + "\t" + value.data + "\t" + value.rozmiar + "\t" + value.type);
- }
- ois.close();
- fis.close();
- }
- private static void readto(String path) throws Exception
- {
- FileOutputStream fos = new FileOutputStream(path);
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(zbior);
- oos.close();
- fos.close();
- }
- }
- class obiekt implements Serializable
- {
- String nazwa;
- long rozmiar;
- String data;
- String type;
- public obiekt(String n, String d, long r, String t)
- {
- nazwa = n;
- data = d;
- rozmiar = r;
- type = t;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement