注册微信号的网站,wordpress cms教程,福州做商城网站公司,网站开发图片框一.对象序列化#xff1a;
1.对象流#xff1a;
ObjectInputStream 和 ObjectOutputStream
2.作用#xff1a;
ObjectOutputSteam#xff1a;内存中的对象--存储中的文件#xff0c;通过网络传输出去 ObjectInputStream:存储中的文件#xff0c;通过网络传输出去…一.对象序列化
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.使用说明
1RandomAccessFile直接继承于java.lang.Object类实现了数据读取和输出
2RandomAccessFile即可以是输入流又可以是输出流
3RandomAccessFile作为输出流时写出文件如果不存在则在执行过程中自动创建
如果写出的文件存在则会对原文件内容进行覆盖
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));}//把指针从写指到3acf1.seek(3);acf1.write(xyz.getBytes());acf1.write(sb.toString().getBytes());acf1.close();}
三.网络传输
1.实现网络通信需要解决的两个问题
1如何准确地定位网络上一台或多台主机定位主机上的特定应用
2找到主机后如何可靠高效地进行数据传输
2.网络通信的两个要素
1对应问题一ip和端口号
2对应问题二提供网络通信协议TCP/IP参考模型应用层传输层网络层物理数据链路层
3.通信要素一ip和端口号
1ip的理解
》ip唯一标识Internet上计算机通信实体
》在java 中使用InetAddress类代表ip
》ip分类ipv4和ipv6 万维网和局域网
》域名www.baidu.com ,www.jd.com
2InetAddress类
2.1 实例化
getByName(String host),getLocalHost()
2.2 常用方法
getHostName() /getHostAddress()
3.端口号正在计算机上运行的进程
要求不同的进程不同的端口号
范围被规定为一个正整数 0-65535
端口号与ip地址组合得出一个网络套接字Socket
4.通信要素二网络通信协议
1分类模型 2TCP和UDP的区别
TCP协议使用协议前建立TCP连接形成传输通道传输前使用三次握手方式点对点通信是可靠的在连接中可进行大数据量的传输传输完毕需释放已建立的连接效率低
UDP协议将数据源目的封装成数据包不需要建立连接每个数据报的大小限制在64k内
发送不管对方是否准备好接收方收到也不确认故是不可靠的可以广播发送发送数据结束时无需释放资源开销小速度快
5.代码实例
1IP地址代码实例
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());}
}
2TCP代码实例
public class Tcp1Test {
//发送信息Testpublic 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();}Testpublic 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((leninputStream.read(buffer))!-1){byteout.write(buffer,0,len);System.out.println(byteout.toString());}server.close();inputStream.close();byteout.close();}}public class Tcp2Test {//发送文件Testpublic 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((lenfis.read(buffer))!-1){outputStream.write(buffer,0,len);}fis.close();outputStream.close();clent.close();}Testpublic void test2() throws IOException {ServerSocket server new ServerSocket(8851);Socket accept server.accept();InputStream inputStream accept.getInputStream();FileOutputStream fosnew FileOutputStream(leaf2.jpg);byte[] bufferr new byte[10];int len;while((leninputStream.read(bufferr))!-1){fos.write(bufferr,0,len);}fos.close();inputStream.close();server.close();}}
public class Tcp3Test {//发送文件服务器返回信息Testpublic 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((lenfis.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((len1inputStream.read(bu))!-1){bos.write(bu,0,len1);}System.out.println(bos.toString());fis.close();outputStream.close();client.close();}Testpublic 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((leninputStream.read(buff))!-1){fos.write(buff,0,len);}//给客户端发送已接收消息OutputStream outputStream accept.getOutputStream();outputStream.write(图片已收到谢谢.getBytes());fos.close();inputStream.close();server.close();outputStream.close();}}
3UDP代码实例
public class UdpTest {Testpublic 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();}Testpublic 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()));}
}4URL代码实例
public class UrlTest {Testpublic 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());//获取查询内容}Testpublic 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((leninputStream.read(buffer))!-1){fos.write(buffer,0,len);}inputStream.close();fos.close();urlConnection.disconnect();}
}