Created
February 21, 2011 22:09
-
-
Save damienix/837794 to your computer and use it in GitHub Desktop.
Simple file browser interface.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package przyklady; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.ObjectInputStream; | |
import java.io.ObjectOutputStream; | |
import java.io.Serializable; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import java.util.HashSet; | |
import java.util.Set; | |
import java.util.TreeSet; | |
/** | |
* | |
* @author student | |
*/ | |
public class FileBrowser implements Serializable, Comparable<FileBrowser> { | |
private Set<FileBrowser> files; | |
private String filename; | |
private Date modified; | |
long size; | |
Type type; | |
@Override | |
public int compareTo(FileBrowser o) { | |
return this.filename.compareTo(o.filename); | |
} | |
public enum Type { | |
FILE, | |
DIR; | |
@Override | |
public String toString() { | |
switch (this) { | |
case FILE: | |
return "F"; | |
case DIR: | |
return "D"; | |
} | |
return null; | |
} | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj == null) { | |
return false; | |
} | |
if (getClass() != obj.getClass()) { | |
return false; | |
} | |
final FileBrowser other = (FileBrowser) obj; | |
if (this.files != other.files && (this.files == null || !this.files.equals(other.files))) { | |
return false; | |
} | |
if ((this.filename == null) ? (other.filename != null) : !this.filename.equals(other.filename)) { | |
return false; | |
} | |
if (this.modified != other.modified && (this.modified == null || !this.modified.equals(other.modified))) { | |
return false; | |
} | |
if (this.size != other.size) { | |
return false; | |
} | |
if (this.type != other.type) { | |
return false; | |
} | |
return true; | |
} | |
@Override | |
public int hashCode() { | |
int hash = 7; | |
hash = 89 * hash + (this.filename != null ? this.filename.hashCode() : 0); | |
hash = 89 * hash + (this.modified != null ? this.modified.hashCode() : 0); | |
hash = 89 * hash + (int) (this.size ^ (this.size >>> 32)); | |
return hash; | |
} | |
@Override | |
public String toString() { | |
String ret = ""; | |
String prefix = ">"; | |
SimpleDateFormat format = | |
new SimpleDateFormat("yyyy-MM-d HH:mm"); | |
ret += String.format("%s %s %d\n", prefix + filename, type, size, format.format(modified)); | |
for (FileBrowser f : files) { | |
ret += f.toString(prefix); | |
} | |
return ret; | |
} | |
public String toString(String prefix) { | |
String ret = ""; | |
prefix += ">"; | |
SimpleDateFormat format = | |
new SimpleDateFormat("yyyy-MM-d HH:mm"); | |
ret += String.format("%s %s %d %s\n", prefix + filename, type, size, format.format(modified)); | |
for (FileBrowser f : files) { | |
ret += f.toString(prefix); | |
} | |
return ret; | |
} | |
public FileBrowser() { | |
} | |
public FileBrowser(String[] opts) { | |
files = opts[1].equals("sorted") ? new TreeSet<FileBrowser>() : new HashSet<FileBrowser>(); | |
File f = new File(opts[0]); | |
filename = f.getName(); | |
modified = new Date(f.lastModified()); | |
size = f.isFile() ? f.length() : f.list().length; | |
type = f.isFile() ? FileBrowser.Type.FILE : FileBrowser.Type.DIR; | |
if (f.isDirectory()) { | |
for (String file : f.list()) { | |
String[] newOpts = new String[3]; | |
newOpts[0] = opts[0] + "/" + file; | |
newOpts[1] = opts[1]; | |
FileBrowser nextFile = new FileBrowser(newOpts); | |
files.add(nextFile); | |
} | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
FileBrowser f = null; | |
// f = new FileBrowser(args); | |
// | |
// ObjectOutputStream oos = null; | |
// FileOutputStream fos = new FileOutputStream("car.ser"); | |
// oos = new ObjectOutputStream(fos); | |
// oos.writeObject(f); | |
ObjectInputStream ois = null; | |
FileInputStream fis = new FileInputStream("car.ser"); | |
ois = new ObjectInputStream(fis); | |
f = (FileBrowser) ois.readObject(); | |
System.out.println(f); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment