允许把内存中的Java对象转换成与平台无关的二进制流,从而允许把这种二进制流持久的保存在磁盘上,或通过网络将这种二进制传输到另一个网络节点,
当其他程序获取这种二进制流,就可以恢复为原来的Java对象
序列化:将内存中的Java对象转换成与平台无关的二进制流
public class test {
public static void main(String[] args) {
ObjectOutputStream oos = null;
try {
FileOutputStream fileOutputStream = new FileOutputStream("dest.dat");
oos = new ObjectOutputStream(fileOutputStream);
oos.writeObject("我爱中国,我爱共产党");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
反序列化:将磁盘文件中的对象还原为内存中的一个java对象
public class TestJie {
public static void main(String[] args) {
ObjectInputStream ois = null;
try {
FileInputStream fileInputStream = new FileInputStream("dest.dat");
ois = new ObjectInputStream(fileInputStream);
Object o = ois.readObject();
String str = (String) o;
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
如果对象流要传的数据为自定义类
我们要将自定义类型序列化
1.需要实现接口:Serializable
2.当前类提供一个全局变量:serialVersionUID
执行如上两部即可序列化
3.除了当前的类需要实例化外,必须保证内部的所有属性都必须是可实例化的(默认情况下,基本数据类型都是可实例化的)
补充:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
1.RandomAccessFile直接继承java.lang.Object类,实现了DataInput和DataOutput接口
2.RandomAccessFile即可作为输入流也可以作为输出流,却决于构造器是 r还是rw
3.如果RandomAccessFiLe作为输出流时,写出到的文件如果不存在,则在执行过程中自动创建
如果写出到的文件存在,则会对原有文件内容自行覆盖。(默认情况下,从头覆盖)
seek方法:将指针调到角标为几的位置,write方法将从指定角标位置进行覆盖
RandomAccessFile类应用:多线程下载
main方法中文件的相对路径为Poject类
Junit测试在文件的相对路径在当前的Module下
我们一般在开发时会使用第三方jar包来简化开发
NIO是IO的进阶产品
NIO2因为NIO不太好用,NIO2出现
NIO2中Path相当于IO中的File且二者可以相互转换
直接或间接地通过网络协议与其他计算机实现数据交互,进行通信
如何准确地定位网络上一台主机或多台主机;定位主机上的特定应用
找到主机后如何高效进行数据传输
在Java中使用InetAddress类表示IP
唯一的标识Internet上的计算机(通信实体)
本地回环地址:127.0.0.1 主机名(hostName):localhost
IP分类:IPV4与IPV6;万维网和局域网
域名:www.baidu.com www.mi.com
实例化InetAddress的两个方法:getByName(String host)
getLocalHost():获取本地IP地址
常用的两个方法getHostName() getHostAddress()
正在计算机上运行的进程
要求:不同的进程由不同的端口号
范围:被规定为16位的整数0~65535
代表一台主机上不同的应用程序
TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
端口号与IP地址的组合得出一个网络套接字:Socket
1.创建Socket对象,指明服务器端的ip的端口号
2.获取一个输出流,用于输出数据
3.写入数据的操作
4.资源的关闭
@Test
public void client() {
Socket socket = null;
OutputStream os = null;
try {
InetAddress inetAddress = InetAddress.getByName("192.168.100.101");
socket = new Socket(inetAddress, 8888);
os = socket.getOutputStream();
os.write("我爱你中国,我爱你刘晶晶".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.创建服务器端的ServerSocket,指明自己的窗口号
2.调用accept()表示接收来自于客户端的Socket
3.获取输入流
4.读取输入流中的数据
//服务端
@Test
public void service() {
ServerSocket serverSocket = null;
Socket accept = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
serverSocket = new ServerSocket(8888);
accept = serverSocket.accept();
is = accept.getInputStream();
baos = new ByteArrayOutputStream();
byte[] bytes = new byte[5];
int len;
while ((len = is.read(bytes)) != -1) {
baos.write(bytes, 0, len);
}
System.out.println(baos.toString());
System.out.println("收到了来自于:" + serverSocket.getInetAddress().getHostAddress() + "的数据");
} catch (IOException e) {
e.printStackTrace();
} finally {
if(baos != null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (accept != null) {
try {
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if ( serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
升级版:客户端向服务器端发送文件,服务器端将文件保存在本地,之后给客户端反馈,客户端将反馈信息打印到控制台
客户端
//客户端
@Test
public void client() {
OutputStream outputStream = null;
FileInputStream fileInputStream = null;
Socket socket = null;
ByteArrayOutputStream stream=null;
try {
socket = new Socket("127.0.0.1", 8999);
outputStream = socket.getOutputStream();
fileInputStream = new FileInputStream("1.jpg");
byte[] bytes = new byte[10];
int len;
while ((len = fileInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, len);
}
socket.shutdownOutput();
InputStream ips = socket.getInputStream();
stream = new ByteArrayOutputStream();
byte[] bytess = new byte[10];
int lens;
while ((lens= ips.read(bytess))!=-1){
stream.write(bytess,0,lens);
}
System.out.println(stream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.read();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(stream!=null){
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
服务端
//服务端
@Test
public void service() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream outputStream=null;
try {
serverSocket = new ServerSocket(8999);
socket = serverSocket.accept();
is = socket.getInputStream();
fos = new FileOutputStream("3.png");
byte[] bytes = new byte[10];
int len;
while ((len = is.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
outputStream = socket.getOutputStream();
outputStream.write("收到你的数据了!!!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.实例化DatagramSocket类
2.实例化DatagramPacket,使用构造器指定数据以及地址
3.调用实例化DatagramSocket类的方法send/recive
4.关闭资源
@Test
//发送端
public void client() {
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket();
String s = "我用UDP导弹干你";
byte[] bytes = s.getBytes();
InetAddress localHost = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, localHost, 8899);
datagramSocket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (datagramSocket != null) {
datagramSocket.close();
}
}
}
接收端
@Test
//接收端
public void service() {
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket(8899);
byte[] bytes = new byte[100];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
datagramSocket.receive(packet);
byte[] data = packet.getData();
String str = new String(data);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
} finally {
datagramSocket.close();
}
}
1.URL:统一资源定位符,对应着互联网的某一资源地址
2.格式
http://localhost:8080/examples/beauty.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表
public String getProtocol()获取该URL的协议名
public String getHost()获取该URL的主机名
public String getport()获取该URL的端口号
public String getPath()获取该URL的文件路径
public String getFile()获取该URL的文件名
public String getQuery()获取该URL的查询名
注意:openConnection()方法要声明为HttpURLConnection
要关闭连接!!!!!!!!!
public class URLPhotoTest {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL("https://img2.baidu.com/it/u=2858376042,4067908290&fm=253&fmt=auto&app=120&f=JPEG?w=640&h=1280");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
inputStream = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream("beauty.png");
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
urlConnection.disconnect();
}
}
}