一.对象序列化:
1.对象流:
ObjectInputStream 和 ObjectOutputStream
2.作用:
ObjectOutputSteam:内存中的对象-->存储中的文件,通过网络传输出去
ObjectInputStream:存储中的文件,通过网络传输出去-->内存中的对象
3.对象的序列化机制:
对象序列化机制允许把内存中的java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另外一个网络节点。//当其他程序获取了这种二进制流,就可以恢复成原来的java对象
4.序列化和反序列化过程:
代码实现:
public static void main(String[] args) throws IOException, ClassNotFoundException {
//创建对象流
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream("object.txt"));
//字符串序列化
oos.writeObject("北京欢迎你!");
oos.flush();
System.out.println("***************************************");
//对象序列化
Person p = new Person("tom",20);
oos.writeObject(p);
oos.flush();
oos.close();
System.out.println("***************************************");
//反序列化:把对象从文件或数据库中读取到内存中
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.txt"));
Object o = ois.readObject();
System.out.println(o);
Object o1 = ois.readObject();
System.out.println(o1);
ois.close();
}
5.实现序列化的对象所属的类需要满足:
1)需要实现接口:Serializable
2)当前类提供一个全局常量:SerialVersionUid
3)除了当前类需要实现Serializable接口之外,还需要保证其内部所有属性也必须是可序列化的
补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
二.随机存取文件流:
1.随机存取文件流:RandomAccessFile
2.使用说明:
1)RandomAccessFile直接继承于java.lang.Object类,实现了数据读取和输出
2)RandomAccessFile即可以是输入流又可以是输出流
3)RandomAccessFile作为输出流时,写出文件如果不存在,则在执行过程中自动创建,
如果写出的文件存在,则会对原文件内容进行覆盖
4)可以通过相关的操作,实现RandomAccessFile"插入"数据的效果。seek(int pos)
3.典型代码:
@Test
public void test() throws IOException {
RandomAccessFile acf1 = new RandomAccessFile("cat.jpg", "r");
RandomAccessFile acf2 = new RandomAccessFile("cat3.jpg", "rw");
byte[] bute = new byte[1024];
int len;
while ((len = acf1.read(bute)) != -1) {
acf2.write(bute, 0, len);
}
acf1.close();
acf2.close();
}
@Test
public void test2() throws IOException {
RandomAccessFile acf1 = new RandomAccessFile("hello1.txt", "rw");
acf1.seek(3);
acf1.write("xyz".getBytes());
acf1.close();
}
@Test
public void test3() throws IOException {
/*
使用 RandomAccessFile 实现插入功能
*/
RandomAccessFile acf1 = new RandomAccessFile("h.txt", "rw");
acf1.seek(3);
StringBuilder sb = new StringBuilder((int)new File("h.txt").length());
//把剩余内容存到StringBuilder对象
byte[] bute = new byte[1024];
int len;
while ((len = acf1.read(bute)) != -1) {
sb.append(new String(bute,0,len));
}
//把指针从写指到3
acf1.seek(3);
acf1.write("xyz".getBytes());
acf1.write(sb.toString().getBytes());
acf1.close();
}
三.网络传输:
1.实现网络通信需要解决的两个问题
1)如何准确地定位网络上一台或多台主机:定位主机上的特定应用
2)找到主机后如何可靠高效地进行数据传输
2.网络通信的两个要素:
1)对应问题一:ip和端口号
2)对应问题二:提供网络通信协议:TCP/IP参考模型(应用层,传输层,网络层,物理+数据链路层)
3.通信要素一:ip和端口号
1)ip的理解
》ip:唯一标识Internet上计算机(通信实体)
》在java 中使用InetAddress类代表ip
》ip分类:ipv4和ipv6 ;万维网和局域网
》域名:www.baidu.com ,www.jd.com
2)InetAddress类:
2.1 实例化
getByName(String host),getLocalHost()
2.2 常用方法
getHostName() /getHostAddress()
3.端口号:正在计算机上运行的进程
要求:不同的进程不同的端口号
范围:被规定为一个正整数 0-65535
端口号与ip地址组合得出一个网络套接字:Socket
4.通信要素二:网络通信协议:
1)分类模型
2)TCP和UDP的区别
TCP协议:使用协议前建立TCP连接,形成传输通道,传输前使用三次握手方式,点对点通信,是可靠的;在连接中可进行大数据量的传输;传输完毕,需释放已建立的连接,效率低
UDP协议:将数据,源,目的封装成数据包,不需要建立连接,每个数据报的大小限制在64k内;
发送不管对方是否准备好,接收方收到也不确认,故是不可靠的;可以广播发送;发送数据结束时,无需释放资源,开销小,速度快
5.代码实例:
1)IP地址代码实例:
public class InetAddressTest {
public static void main(String[] args) throws UnknownHostException {
//根据ip地址创建ip地址对象
InetAddress ina1 =InetAddress.getByName("192.168.1.1");
System.out.println(ina1);
//根据域名创建ip地址对象
InetAddress ina2= InetAddress.getByName("www.baidu.com");
System.out.println(ina2);
byte[] address = ina1.getAddress();
System.out.println(ina1.getHostName());
System.out.println(ina1.getHostAddress());
System.out.println(ina2.getHostName());
System.out.println(ina2.getHostAddress());
}
}
2)TCP代码实例:
public class Tcp1Test {
//发送信息
@Test
public void test() throws IOException {
Socket clent = new Socket(InetAddress.getByName("127.0.0.1"),8899);
OutputStream outputStream = clent.getOutputStream();
outputStream.write("我是客户端".getBytes());
outputStream.close();
clent.close();
}
@Test
public void test2() throws IOException {
ServerSocket server =new ServerSocket(8899);
Socket accept = server.accept();
InputStream inputStream = accept.getInputStream();
ByteArrayOutputStream byteout =new ByteArrayOutputStream();
byte[] buffer =new byte[20];
int len;
while((len=inputStream.read(buffer))!=-1){
byteout.write(buffer,0,len);
System.out.println(byteout.toString());
}
server.close();
inputStream.close();
byteout.close();
}
}
public class Tcp2Test {
//发送文件
@Test
public void test1() throws IOException {
InetAddress inet= InetAddress.getByName("127.0.0.1");
Socket clent =new Socket(inet,8851);
OutputStream outputStream = clent.getOutputStream();
FileInputStream fis= new FileInputStream("leaf.jpg");
byte[] buffer= new byte[10];
int len;
while((len=fis.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
fis.close();
outputStream.close();
clent.close();
}
@Test
public void test2() throws IOException {
ServerSocket server =new ServerSocket(8851);
Socket accept = server.accept();
InputStream inputStream = accept.getInputStream();
FileOutputStream fos=new FileOutputStream("leaf2.jpg");
byte[] bufferr = new byte[10];
int len;
while((len=inputStream.read(bufferr))!=-1){
fos.write(bufferr,0,len);
}
fos.close();
inputStream.close();
server.close();
}
}
public class Tcp3Test {
//发送文件,服务器返回信息
@Test
public void test() throws IOException {
InetAddress inet = InetAddress.getByName("127.0.0.1");
Socket client =new Socket(inet,7788);
OutputStream outputStream = client.getOutputStream();
//从本地读取文件并发送到服务器
FileInputStream fis =new FileInputStream("leaf.jpg");
byte[] buffer =new byte[20];
int len;
while((len=fis.read(buffer))!=-1){
outputStream.write(buffer,0,len);
}
//关闭数据输出
client.shutdownOutput();
//接受服务器发送的信息,并输出到控制台
InputStream inputStream = client.getInputStream();
ByteArrayOutputStream bos =new ByteArrayOutputStream();
byte[] bu =new byte[20];
int len1;
while((len1=inputStream.read(bu))!=-1){
bos.write(bu,0,len1);
}
System.out.println(bos.toString());
fis.close();
outputStream.close();
client.close();
}
@Test
public void test2() throws IOException {
ServerSocket server = new ServerSocket(7788);
Socket accept = server.accept();
InputStream inputStream = accept.getInputStream();
//接受客户端发送文件并存到本地
FileOutputStream fos= new FileOutputStream("leaf3.jpg");
byte[] buff =new byte[20];
int len;
while((len=inputStream.read(buff))!=-1){
fos.write(buff,0,len);
}
//给客户端发送已接收消息
OutputStream outputStream = accept.getOutputStream();
outputStream.write("图片已收到,谢谢".getBytes());
fos.close();
inputStream.close();
server.close();
outputStream.close();
}
}
3)UDP代码实例:
public class UdpTest {
@Test
public void test() throws IOException {
//发送端
DatagramSocket socket =new DatagramSocket();
InetAddress inet =InetAddress.getLocalHost();
String str="我是udp方式发送的数据";
byte[] buff = str.getBytes();
DatagramPacket packet =new DatagramPacket(buff,0,buff.length,inet,8890);
socket.send(packet);
socket.close();
}
@Test
public void test2() throws IOException {
//接收者
DatagramSocket socket =new DatagramSocket(8890);
byte[] buffer =new byte[100];
DatagramPacket packet =new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
}
}
4)URL代码实例:
public class UrlTest {
@Test
public void test() throws MalformedURLException {
//获取url对象
URL url =new URL("http://localhost:8080/examples/leaf.jpg");
//url 主要方法
System.out.println(url.getProtocol());//获取协议
System.out.println(url.getHost());//获取主机
System.out.println(url.getPort());//获取端口
System.out.println(url.getQuery());//获取查询内容
}
@Test
public void test2() throws IOException {
//服务器下载图片
URL url =new URL("http://localhost:8080/examples/leaf.jpg");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
byte[] buffer =new byte[20];
int len;
FileOutputStream fos =new FileOutputStream("leafdown.jpg");
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
inputStream.close();
fos.close();
urlConnection.disconnect();
}
}