package com.evs.objava33.class11;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class TestFile {
public static void main(String[] args) {
File myFile = new File("d:/abc.txt");
// File myDir = new File("c:/abc");
// File myDirFile = new File(myDir, "File.java");
// File myDirFile1 = new File("c:/abc", "File.java");
//
// try {
// File remoteFile = new File(new URI("http://www.yahoo.com/abc.html"));
// } catch (URISyntaxException e) {
// e.printStackTrace();
// }
String userDir = System.getProperty("user.dir");
// File dDrive = new File("D:" + File.pathSeparator + "temp-read");
File dDrive = new File("D:/temp-read");
String[] files = dDrive.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith("txt");
}
});
File[] files1 = dDrive.listFiles(new FileFilter() {
public boolean accept(File pathname) {
return true;
}
});
for (String s : files) {
System.out.println(s);
/*
* abc.txt Solaris10_9-10_VM.zip WorkFlowDAOImpl.class
*/
}
// try {
// BufferedOutputStream out = new BufferedOutputStream(
// new FileOutputStream(myFile, true));
// out.write("this is my first file".getBytes());
// out.close();
//
// // RandomAccessFile raf = new RandomAccessFile(myFile, "rw");
// // raf.write("This is my first File".getBytes());
// // raf.close();
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// try {
// Process p = Runtime.getRuntime().exec("ping 192.168.0.1");
// } catch (IOException e) {
// e.printStackTrace();
// }
ObjectOutputStream fout = null;
try {
fout = new ObjectOutputStream(new FileOutputStream(myFile));
fout.writeObject(new TestObject(1, "One"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
ObjectInputStream fin = null;
try {
fin = new ObjectInputStream(new FileInputStream(myFile));
Object obj = fin.readObject();
if (obj instanceof TestObject) {
TestObject testObj = (TestObject) obj;
System.out.println(testObj);
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private static class TestObject implements Serializable {
private static final long serialVersionUID = -4314877130551524995L;
private String value;
private Integer key;
private transient String anotherString;
public TestObject(Integer key, String value) {
this.key = key;
this.value = value;
this.anotherString = "Unknown";
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TestObject [value=" + value + ", key=" + key
+ ", anotherString=" + anotherString + "]";
}
/**
* @return the value
*/
public String getValue() {
return value;
}
/**
* @param value
* the value to set
*/
public void setValue(String value) {
this.value = value;
}
/**
* @return the key
*/
public Integer getKey() {
return key;
}
/**
* @param key
* the key to set
*/
public void setKey(Integer key) {
this.key = key;
}
}
}