IO
对象流
用于存储和读取基本数据类型或对象的处理流 序列化 ObjectOutputStream 保存基本数据类型或对象 反序列化 ObjectInputStream 读取基本数据类型或对象 如果对象是序列化的,则对象需要实现Serializable,再需要当前提供一个全局serialVersionID常量,并且类的属性必须都是可以序列化的
public class Test {
public static void main ( String[ ] args) throws IOException, ClassNotFoundException {
out ( "你好" , "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/test.txt" ) ;
String str = ( String) in ( "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/test.txt" ) ;
System. out. println ( str) ;
out ( new Person ( "小花" , 12 ) , "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/Person.txt" ) ;
Person person = ( Person) in ( "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/Person.txt" ) ;
System. out. println ( person) ;
}
public static void out ( Object obj, String path1) throws IOException {
ObjectOutputStream oos = new ObjectOutputStream ( new FileOutputStream ( path1) ) ;
oos. writeObject ( obj) ;
oos. close ( ) ;
}
public static Object in ( String path2) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream ( new FileInputStream ( path2) ) ;
Object obj = ois. readObject ( ) ;
ois. close ( ) ;
return obj;
}
}
public class Person implements Serializable {
public static final long serialVersionUID = 1232343452345 L;
String name;
int age;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
@Override
public String toString ( ) {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}' ;
}
public Person ( ) {
}
public Person ( String name, int age) {
this . name = name;
this . age = age;
}
public void setAge ( int age) {
this . age = age;
}
}
随机存取文件流
RandomAccessFile继承与Object,实现了DataOutput和DataInput等接口,既可以作为输出流,又可以作为输入流 mode, r为读,rw为读写
public class Test1 {
public static void main ( String[ ] args) throws IOException {
RandomAccessFile raf = new RandomAccessFile ( "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/Person.txt" , "r" ) ;
byte [ ] data = new byte [ 1024 ] ;
int len;
while ( ( len = raf. read ( data) ) != - 1 ) {
String str = new String ( data, 0 , len) ;
System. out. print ( str) ;
}
raf. close ( ) ;
}
}
网络
直接或间接的通过网络协议与其他计算机实现数据交换,进行通讯 通信要素 1. IPV4 和 IPV6 2. DNS解析 3. 本机地址 127.0.0.1 对应 localhost
public class Test {
public static void main ( String[ ] args) throws UnknownHostException {
InetAddress str = InetAddress. getByName ( "www.sina.com" ) ;
System. out. println ( str) ;
System. out. println ( InetAddress. getLocalHost ( ) ) ;
}
}
端口号 同一台电脑的端口不能重复,不同进程有不同端口器
TCP
Socket传输字符
public class Test11 {
public static void main ( String[ ] args) throws IOException {
server ( ) ;
}
public static void server ( ) throws IOException {
ServerSocket socket = new ServerSocket ( 1120 ) ;
Socket socket1 = socket. accept ( ) ;
InputStream os = socket1. getInputStream ( ) ;
byte [ ] data = new byte [ 1024 ] ;
int len;
while ( ( len = os. read ( data) ) != - 1 ) {
String str = new String ( data, 0 , len) ;
System. out. println ( str) ;
}
os. close ( ) ;
socket. close ( ) ;
}
}
public class Test22 {
public static void main ( String[ ] args) throws IOException {
client ( ) ;
}
public static void client ( ) throws IOException {
InetAddress inetAddress = InetAddress. getByName ( "localhost" ) ;
Socket socket = new Socket ( inetAddress, 1120 ) ;
OutputStream os = socket. getOutputStream ( ) ;
os. write ( "nihao" . getBytes ( ) ) ;
os. close ( ) ;
socket. close ( ) ;
}
}
Socket传输文件
public class Test22 {
public static void main ( String[ ] args) throws IOException {
client ( ) ;
}
public static void client ( ) throws IOException {
InetAddress inetAddress = InetAddress. getByName ( "localhost" ) ;
Socket socket = new Socket ( inetAddress, 1120 ) ;
OutputStream os = socket. getOutputStream ( ) ;
BufferedInputStream bis = new BufferedInputStream ( new FileInputStream ( "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/复习.txt" ) ) ;
byte [ ] data = new byte [ 1024 ] ;
int len;
while ( ( len = bis. read ( data) ) != - 1 ) {
os. write ( data, 0 , len) ;
}
bis. close ( ) ;
os. close ( ) ;
socket. close ( ) ;
}
}
public class Test11 {
public static void main ( String[ ] args) throws IOException {
server ( ) ;
}
public static void server ( ) throws IOException {
ServerSocket socket = new ServerSocket ( 1120 ) ;
Socket socket1 = socket. accept ( ) ;
InputStream os = socket1. getInputStream ( ) ;
BufferedOutputStream bos = new BufferedOutputStream ( new FileOutputStream ( "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/复习_server.txt" ) ) ;
byte [ ] data = new byte [ 1024 ] ;
int len;
while ( ( len = os. read ( data) ) != - 1 ) {
bos. write ( data, 0 , len) ;
}
bos. close ( ) ;
os. close ( ) ;
socket. close ( ) ;
}
}
Socket传输文件并返回信息
public class Test11 {
public static void main ( String[ ] args) throws IOException {
server ( ) ;
}
public static void server ( ) throws IOException {
ServerSocket socket = new ServerSocket ( 1120 ) ;
Socket socket1 = socket. accept ( ) ;
InputStream os = socket1. getInputStream ( ) ;
BufferedOutputStream bos = new BufferedOutputStream ( new FileOutputStream ( "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/复习_server_two.txt" ) ) ;
byte [ ] data = new byte [ 1024 ] ;
int len;
while ( ( len = os. read ( data) ) != - 1 ) {
bos. write ( data, 0 , len) ;
}
OutputStream outputStream = socket1. getOutputStream ( ) ;
outputStream. write ( "文件已经成功收到了" . getBytes ( ) ) ;
outputStream. close ( ) ;
bos. close ( ) ;
os. close ( ) ;
socket. close ( ) ;
}
}
public class Test22 {
public static void main ( String[ ] args) throws IOException {
client ( ) ;
}
public static void client ( ) throws IOException {
InetAddress inetAddress = InetAddress. getByName ( "localhost" ) ;
Socket socket = new Socket ( inetAddress, 1120 ) ;
OutputStream os = socket. getOutputStream ( ) ;
BufferedInputStream bis = new BufferedInputStream ( new FileInputStream ( "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/src/复习.txt" ) ) ;
byte [ ] data = new byte [ 1024 ] ;
int len;
while ( ( len = bis. read ( data) ) != - 1 ) {
os. write ( data, 0 , len) ;
}
socket. shutdownOutput ( ) ;
InputStream inputStream = socket. getInputStream ( ) ;
byte [ ] data1 = new byte [ 1024 ] ;
int len1;
while ( ( len1 = inputStream. read ( data1) ) != - 1 ) {
String str = new String ( data1, 0 , len1) ;
System. out. println ( str) ;
}
inputStream. close ( ) ;
bis. close ( ) ;
os. close ( ) ;
socket. close ( ) ;
}
}
URL
URL <传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
使用URL下载图片
public class Test {
public static void main ( String[ ] args) throws IOException {
URL url = new URL ( "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3198630764,2313681489&fm=26&gp=0.jpg" ) ;
System. out. println ( url. getHost ( ) ) ;
System. out. println ( url. getPort ( ) ) ;
System. out. println ( url. getProtocol ( ) ) ;
System. out. println ( url. getDefaultPort ( ) ) ;
System. out. println ( url. getPath ( ) ) ;
HttpURLConnection urlConnection = ( HttpURLConnection) url. openConnection ( ) ;
urlConnection. connect ( ) ;
InputStream inputStream = urlConnection. getInputStream ( ) ;
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream ( new FileOutputStream ( "/Users/guochongling/WorkSpace/IDEA/JAVA_SE/day10/图片.jpg" ) ) ;
byte [ ] data = new byte [ 1024 ] ;
int len;
while ( ( len = inputStream. read ( data) ) != - 1 ) {
bufferedOutputStream. write ( data, 0 , len) ;
}
bufferedOutputStream. close ( ) ;
inputStream. close ( ) ;
urlConnection. disconnect ( ) ;
}
}