黑马程序员---2015.6.27java基础笔记--字符编码--TCP--UDP

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

1.操作基本数据类型

        DataInputStream和DataOutputStream
    操作字节数组
        ByteArrayInputStream和ByteArrayOutputStream
    操作字符数组
        CharArrayReader和CharArrayWriter
    操作字符串
        StringReader和StringWriter
        
2.ByteArrayStream
/*
用于操作字节数组的流对象。

ByteArrayInputStream :在构造的时候,需要接收数据源,而且数据源是一个字节数组。

ByteArrayOutputStream: 在构造的时候,不用定义数据目的,因为该对象中已经内部封装了可变长度的字节数组。
这就是数据目的地。

因为这两个流对象都操作的数组,并没有使用系统资源。
所以,不用进行close关闭。

在流操作规律讲解时:

源设备,
    键盘 System.in,硬盘 FileStream,内存 ArrayStream。
目的设备:
    控制台 System.out,硬盘FileStream,内存 ArrayStream。


用流的读写思想来操作数据。


*/
import java.io.*;
class ByteArrayStream
{
    public static void main(String[] args)
    {
        //数据源。
        ByteArrayInputStream bis = new ByteArrayInputStream("ABCDEFD".getBytes());

        //数据目的
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        int by = 0;

        while((by=bis.read())!=-1)
        {
            bos.write(by);
        }



        System.out.println(bos.size());
        System.out.println(bos.toString());

    //    bos.writeTo(new FileOutputStream("a.txt"));

    }
}


3.字符编码

import java.io.*;

class EncodeStream
{
    public static void main(String[] args) throws IOException
    {
            //writeText();
            readText();
    }

    public static void readText()throws IOException
    {
        InputStreamReader isr = new InputStreamReader(new FileInputStream("utf.txt"),"gbk");

        char[] buf = new char[10];
        int len = isr.read(buf);

        String str = new String(buf,0,len);

        System.out.println(str);

        isr.close();
    }
    public static void writeText()throws IOException
    {
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("utf.txt"),"UTF-8");

        osw.write("你好");

        osw.close();
    }
}



4.在记事本中输入“联通”两字,再打开的时候,会乱码。
    因为存储的时候,将中文查找GBK码表,将查的值的2进制形式存储起来。
    但是再次打开文件的时候,查看2进制,发现像UTF-8的格式,所以就查找utf-8,解码。所以乱码。
    这是特殊的情况。
    
5.
/*
有五个学生,每个学生有3门课的成绩,
从键盘输入以上数据(包括姓名,三门课成绩),
输入的格式:如:zhagnsan,30,40,60计算出总成绩,
并把学生的信息和计算出的总分数高低顺序存放在磁盘文件"stud.txt"中。

1,描述学生对象。
2,定义一个可操作学生对象的工具类。

思想:
1,通过获取键盘录入一行数据,并将该行中的信息取出封装成学生对象。
2,因为学生有很多,那么就需要存储,使用到集合。因为要对学生的总分排序。
    所以可以使用TreeSet。
3,将集合的信息写入到一个文件中。
*/

import java.io.*;
import java.util.*;

class Student implements Comparable<Student>
{
    private String name;
    private int ma,cn,en;
    private int sum;

    Student(String name,int ma,int cn,int en)
    {
        this.name = name;
        this.ma = ma;
        this.cn = cn;
        this.en = en;
        sum = ma + cn + en;
    }


    public int compareTo(Student s)
    {
        int num = new Integer(this.sum).compareTo(new Integer(s.sum));
        if(num==0)
            return this.name.compareTo(s.name);
        return num;
    }



    public String getName()
    {
        return name;
    }
    public int getSum()
    {
        return sum;
    }

    public int hashCode()
    {
        return name.hashCode()+sum*78;

    }
    public boolean equals(Object obj)
    {
        if(!(obj instanceof Student))
            throw new ClassCastException("类型不匹配");
        Student s = (Student)obj;

        return this.name.equals(s.name) && this.sum==s.sum;
    }

    public String toString()
    {
        return "student["+name+", "+ma+", "+cn+", "+en+"]";
    }
}

class StudentInfoTool
{
    
    
    
    public static Set<Student> getStudents()throws IOException
    {
        return getStudents(null);
    }

    public static Set<Student> getStudents(Comparator<Student> cmp)throws IOException
    {
        BufferedReader bufr =
            new BufferedReader(new InputStreamReader(System.in));

        String line = null;
        
        Set<Student> stus  = null;
        if(cmp==null)
            stus = new TreeSet<Student>();
        else
            stus = new TreeSet<Student>(cmp);
        while((line=bufr.readLine())!=null)
        {
            if("over".equals(line))
                break;
            
            String[] info = line.split(",");
            
            Student stu = new Student(info[0],Integer.parseInt(info[1]),
                                        Integer.parseInt(info[2]),
                                        Integer.parseInt(info[3]));

            
            stus.add(stu);
        }

        bufr.close();
        return stus;
    }

    public static void write2File(Set<Student> stus)throws IOException
    {
        BufferedWriter bufw = new BufferedWriter(new FileWriter("stuinfo.txt"));

        for(Student stu : stus)
        {
            bufw.write(stu.toString()+"\t"+"\t"+"\t");
            bufw.write(stu.getSum()+"");
            bufw.newLine();
            bufw.flush();
        }

        bufw.close();

    }
}



class StudentInfoTest
{
    public static void main(String[] args) throws IOException
    {

        Comparator<Student> cmp = Collections.reverseOrder();

        Set<Student> stus = StudentInfoTool.getStudents(cmp);

        StudentInfoTool.write2File(stus);
    }
}



6.网络    
        UDP:面向无连接,将源和目的封装成数据包.
                每个数据包最大小于64K.
                因为无连接,所以不可靠。
                不需要连接,所以速度快。
        TCP:建立连接,形成传输通道。
                连接中可大数据量传输。
                通过三次握手,可靠协议。
                必须建立连接,效率低。
 Socket:为网络服务提供一种机制
                 通信两端都有Socket
                网络通信,其实就是Socket间通讯。
                数据在Socket间通过IO传输
                
    
import java.net.*;
class IPDemo
{
    public static void main(String[] args) throws Exception
    {
        //获取本机IP对象
        InetAddress ia =     InetAddress.getLocalHost();
        //获取ip地址字符串
        String hostAddress = ia.getHostAddress();
        //获取机主名
        String name = ia.getHostName();
        System.out.println("hostAddress:"+hostAddress);
        System.out.println("name:"+name);
        InetAddress[] iad = InetAddress.getAllByName("www.baidu.com");
        for(InetAddress i:iad)
        {
            System.out.println(i.getHostAddress());    
        }
        
    }
}


7.
/*
    UDP发送和接收数据的例子
    但是只能一次发送,一次接收
*/
import java.net.*;
class UdpSend
{
    public static void main(String[] aegs) throws Exception
    {
        //创建udpSocket服务
        DatagramSocket ds = new DatagramSocket(8888);    
        //提供数据并封装进数据包中
        byte[] buff = "hello world".getBytes();
        //要发送的数据内容,长度,目的地址,端口号
        DatagramPacket dp = new DatagramPacket(buff,0,buff.length, InetAddress.getByName("192.168.1.106"),10000) ;
        //socket发送
        ds.send(dp);
        //关闭资源
        ds.close();
    }
}

class UdpReceive
{
    public static void main(String[]  args) throws Exception
    {
        //创建udpSocket服务
        DatagramSocket ds = new DatagramSocket(10000);
        while(true){
        //定义DatagramPacket用于存储收到的字节数据
        byte[] buff = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buff,buff.length);
        
        //通过receive将数据存入DatagramPacket
        ds.receive(dp);//阻塞式方法
        
        //通过DatagramPacket中的特有方法,将数据读取出来
        String ip = dp.getAddress().getHostAddress();
        String data = new String(dp.getData(),0,dp.getLength());
        int port = dp.getPort();
        System.out.println(ip+","+data+","+port);
        }
        //关闭资源
        //ds.close();
    }
}


8.
/*
    UDP发送和接收数据的例子2
    通过键盘录入,发送端可以一直发送,直到关闭。
    接收端一直在接收
*/
import java.net.*;
import java.io.*;
class UdpSend2
{
    public static void main(String[] aegs) throws Exception
    {
        //创建udpSocket服务
        DatagramSocket ds = new DatagramSocket(8888);    
        
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        String  temp = null;
        while((temp=bufr.readLine())!=null)
        {
            if("886".equals(temp))    
                break;
            byte[] b = temp.getBytes();
            DatagramPacket dp = new DatagramPacket(b,0,b.length, InetAddress.getByName("192.168.1.106"),10000) ;    
            ds.send(dp);
        }
        //关闭资源
        ds.close();
    }
}

class UdpReceive2
{
    public static void main(String[]  args) throws Exception
    {
        //创建udpSocket服务
        DatagramSocket ds = new DatagramSocket(10000);
        while(true){
        //定义DatagramPacket用于存储收到的字节数据
        byte[] buff = new byte[1024];
        DatagramPacket dp = new DatagramPacket(buff,buff.length);
        
        //通过receive将数据存入DatagramPacket
        ds.receive(dp);//阻塞式方法
        
        //通过DatagramPacket中的特有方法,将数据读取出来
        String ip = dp.getAddress().getHostAddress();
        String data = new String(dp.getData(),0,dp.getLength());
        int port = dp.getPort();
        System.out.println(ip+","+data+","+port);
        }
        //关闭资源
        //ds.close();
    }
}


9.
import java.io.*;
import java.util.*;
import java.net.*;
/*
编写一个聊天程序。
有收数据的部分,和发数据的部分。
这两部分需要同时执行。
那就需要用到多线程技术。
一个线程控制收,一个线程控制发。

因为收和发动作是不一致的,所以要定义两个run方法。
而且这两个方法要封装到不同的类中。
*/


//发送端
class Send implements Runnable
{
    private DatagramSocket ds = null;
    Send(DatagramSocket ds)
    {
        this.ds = ds;
    }    
    
    public void run()
    {
        try
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));    
            String temp = null;
            while((temp=br.readLine())!=null)
            {
                    if("over".equals(temp))
                        break;
                    byte[] buf = temp.getBytes();
                    DatagramPacket dp = new DatagramPacket(buf,0,buf.length,InetAddress.getByName("192.168.1.106"),10001);
                    ds.send(dp);
            }
            
        }
        catch(Exception e)
        {
            throw new RuntimeException("发送端失败");
        }
        finally
        {
            ds.close();    
        }
    }
}

//接收端
class Receive implements Runnable
{
    private DatagramSocket ds = null;
    Receive(DatagramSocket ds)
    {
        this.ds = ds;
    }    
    
    public void run()
    {
        try
        {
            while(true)
            {
                byte[] buf = new byte[1024];
                DatagramPacket dp = new DatagramPacket(buf,buf.length);
                ds.receive(dp);
                String data = new String(dp.getData(),0,dp.getLength());
                System.out.println("接收端:"+data);
            }
        }
        catch(Exception e)
        {
            throw new RuntimeException("接收端失败");
        }
        finally
        {
            ds.close();    
        }
        
    }
}    

class ChatDemo
{
    public static void main(String[] args) throws Exception
    {
            DatagramSocket ds = new DatagramSocket();
            DatagramSocket ds1 = new DatagramSocket(10001);
            new Thread(new Send(ds)).start();
            new Thread(new Receive(ds1)).start();
    }
}



10.演示TCP传输
/*
演示TCP传输
TCP分为客户端和服务端
客户端对应的对象是:Socket
服务端对应的对象是:ServerSocket

客户端,
通过查阅socket对象,发现在该对象建立时,就可以去连接指定主机。
因为tcp是面向连接的。所以在建立socket服务时,
就要有服务端存在,并连接成功。形成通路后,在该通道进行数据的传输。

需求:给服务端发送给一个文本数据。

步骤:
1,创建Socket服务。并指定要连接的主机和端口。
*/

import java.net.*;
import java.io.*;
class Client
{
    public static void main(String[] args) throws Exception
    {
        //创建Socket服务,并指定主机,和端口号
        Socket s = new Socket("192.168.1.106",10003);
        //为了发送数据,所以得先获取输出流
        OutputStream os = s.getOutputStream();
        os.write("hello world".getBytes());
        //关闭流
        os.close();
    }
}

/*
需求:定义端点接收数据并打印在控制台上。

服务端:
1,建立服务端的socket服务。ServerSocket();
    并监听一个端口。
2,获取连接过来的客户端对象。
    通过ServerSokcet的 accept方法。没有连接就会等,所以这个方法阻塞式的。
3,客户端如果发过来数据,那么服务端要使用对应的客户端对象,并获取到该客户端对象的读取流来读取发过来的数据。
    并打印在控制台。

4,关闭服务端。(可选)
*/
class Server
{
    public static void main(String[] args) throws Exception
    {
        //建立服务端socket,并监听一个端口
        ServerSocket ss = new ServerSocket(10003);
        //获取客户端连接过来的对象
         Socket s = ss.accept();
        //获取客户端对象的流,进行读取数据
        InputStream is = s.getInputStream();
        byte[] b = new byte[1024] ;
        int len = is.read(b);
        System.out.println(new String(b,0,len)+s.getInetAddress().getHostAddress());
        
        ss.close();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值