Java Socket传输数据在进行的时候有很多的事情需要我们不断的进行有关代码的学习只有不断的学习才能掌握相关的问题下面我们就详细的看看如何才能更好的使用这些技术
我们将这个对象串行化至文件系统然后将之还原Java <> Socket传输数据在这个过程其实类似于一个压扁和充气的过程请注意我们的Person类中包含一个嵌入对象并且birthday变化将之设置为transient限定符这表示我们放弃了birthday的串行化
Java代码
package streamdemo;
import javaioByteArrayInputStream;
import javaioByteArrayOutputStream;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javaioObjectInputStream;
import javaioObjectOutputStream;
import javaioOutputStream;
import javautilDate;
public class Persistence {
public static void main(String[] args) {
PersistencesavePerson();
PersistencegetPerson();
}
public static void getPerson() {
try {
InputStream in = new FileInputStream(c:\\persondat);
ObjectInputStream dataInput = new ObjectInputStream(in);
Person p = (Person) dataInputreadObject();
Systemoutprintln(pgetName());
Systemoutprintln(pgetTall());
Systemoutprintln(pgetBirthday());
Systemoutprintln(pgetAddress()getCity());
Systemoutprintln(pgetAddress()getStreet());
} catch (Exception e) {
// TODO Autogenerated catch block
eprintStackTrace();
}
}
public static void savePerson() {
Person p = new Person();
psetName(corey);
psetTall();
psetBirthday(new Date());
psetAddress(new Address(yiyang ziyang));
OutputStream out = new ByteArrayOutputStream();
try {
OutputStream fileOut = new FileOutputStream(new File(
c:\\persondat));
ObjectOutputStream dataOut = new ObjectOutputStream(fileOut);
dataOutwriteObject(p);
dataOutclose();
fileOutclose();
} catch (IOException e) {
// TODO Autogenerated catch block
eprintStackTrace();
}
}
}
package streamdemo;
import javaioByteArrayInputStream;
import javaioByteArrayOutputStream;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javaioInputStream;
import javaioObjectInputStream;
import javaioObjectOutputStream;
import javaioOutputStream;
import javautilDate;
public class Persistence {
public static void main(String[] args) {
PersistencesavePerson();
PersistencegetPerson();
}
public static void getPerson() {
try {
InputStream in = new FileInputStream(c:\\persondat);
ObjectInputStream dataInput = new ObjectInputStream(in);
Person p = (Person) dataInputreadObject();
Systemoutprintln(pgetName());
Systemoutprintln(pgetTall());
Systemoutprintln(pgetBirthday());
Systemoutprintln(pgetAddress()getCity());
Systemoutprintln(pgetAddress()getStreet());
} catch (Exception e) {
// TODO Autogenerated catch block
eprintStackTrace();
}
}
public static void savePerson() {
Person p = new Person();
psetName(corey);
psetTall();
psetBirthday(new Date());
psetAddress(new Address(yiyang ziyang));
OutputStream out = new ByteArrayOutputStream();
try {
OutputStream fileOut = new FileOutputStream(new File(
c:\\persondat));
ObjectOutputStream dataOut = new ObjectOutputStream(fileOut);
dataOutwriteObject(p);
dataOutclose();
fileOutclose();
} catch (IOException e) {
// TODO Autogenerated catch block
eprintStackTrace();
}
}
}
以上就是对Java Socket传输数据的详细介绍希望大家有所收获