码迷,mamicode.com
首页 > 其他好文 > 详细

学习打卡第四天

时间:2019-03-27 00:00:56      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:rgs   服务器   java   writer   generated   generate   oid   host   red   

 1 import java.io.*;
 2 import java.net.InetAddress;
 3 import java.net.Socket;
 4 import java.net.UnknownHostException;
 5 
 6 public class IOTest1 {
 7 
 8     public static void main(String[] args) {
 9         try {
10             //建立一个Scoket对象 
11             Socket socket = new Socket(InetAddress.getByName("localhost"),12345);
12             PrintWriter out = new PrintWriter(
13                     new BufferedWriter(
14                             new OutputStreamWriter(
15                                     socket.getOutputStream())));
16             
17             out.println("hello world");//暂有有一个问题,不知道为何终端接收不到信息
18             BufferedReader in = new BufferedReader(
19                     new InputStreamReader(
20                             socket.getInputStream()));
21             String line = in.readLine();
22             System.out.println(line);
23             in.close();
24             out.close();
25             socket.close();
26         } catch (UnknownHostException e) {
27             // TODO Auto-generated catch block
28             e.printStackTrace();
29         } catch (IOException e) {
30             // TODO Auto-generated catch block
31             e.printStackTrace();
32         } 
33         
34     }
35 
36 }

 

Socket对象是用来通信的,这个代码用来和服务器进行通信,给服务器传达信息。

附截图:技术图片

 1 import java.io.*;
 2 
 3 class Student implements Serializable
 4 {
 5     private String name ;
 6     private int age;
 7     private int grade;
 8     private char sex;
 9     //构建函数
10     public Student (String name,int age,int grade ,char sex)
11     {
12         this.name = name;
13         this.age = age;
14         this.grade = grade;
15         this.sex = sex;
16     }
17     public String toString() //重写函数
18     {
19         return this.name+" "+this.age+" "+this.grade+" "+this.sex;
20     }
21 }
22 public class IOTest2 {
23 
24     public static void main(String[] args) {
25         try {
26             ObjectOutputStream out = new ObjectOutputStream(
27                     new FileOutputStream("a.dat"));
28             Student s1 = new Student("wsy",18,6,‘男‘);
29 //            System.out.println(s1);
30             out.writeObject(s1);
31             out.close();
32             ObjectInputStream in = new ObjectInputStream(
33                     new FileInputStream("a.dat"));
34             //因为read.readObject返回值类型是Object所以要给类型强制类型转换
35             //也要加一个catch捕捉ClassNotFoundException
36             Student s2 = (Student)in.readObject();
37             System.out.println(s2);
38             in.close();
39         } catch (FileNotFoundException e) {
40             // TODO Auto-generated catch block
41             e.printStackTrace();
42         } catch (IOException e) {
43             // TODO Auto-generated catch block
44             e.printStackTrace();
45         } catch (ClassNotFoundException e) {
46             // TODO Auto-generated catch block
47             e.printStackTrace();
48         }
49 
50     }
51 
52 }

以上是我学习的可串化的类,需要去使用接口Serializable,然后就可以直接把一个类写入到一个文件,也可以直接从文件中读出那个类。

学习打卡第四天

标签:rgs   服务器   java   writer   generated   generate   oid   host   red   

原文地址:https://www.cnblogs.com/sucker/p/10604667.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!