IO与网络

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 = 1232343452345L;
    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();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

临水而愚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值