软件计划之Java入门视频(20)

打印 上一主题 下一主题

主题 831|帖子 831|积分 2493

软件计划之Java入门视频(20)

视频教程来自B站尚硅谷:
   尚硅谷Java入门视频教程,宋红康java基础视频
相干文件资料(百度网盘)
提取密码:8op3
idea 下载可以关注 软件管家 公众号
  学习内容:

该视频共分为1-717部门
本次内容涉及 600-629
在写代码时,总是必要来回切换界面来看代码要求,这里推荐Snipaste,可以把截图以窗口形式放在屏幕上
记录内容:

  • 转换流
  • 其他流的利用
  • 对象流
  • 随机存储文件流
1、转换流

   转换流:属于字符流
1、InputStreamReader:将一个字节的输入流转换为字符的输入流
2、OutputStreamWriter:将一个字符的输出流转换为字节的输出流
作用:提供字节省与字符流之间的转换
3、解码:字节、字节数组–>字符数组、字符串
4、编码:字符数组、字符串–>字节、字节数组
  1. @Test
  2.     public void test()  {
  3.         InputStreamReader isr = null;
  4.         try {
  5.             FileInputStream fis = new FileInputStream("hello.txt");
  6.             //参数2指明了字符集,具体使用了哪个字符集,取决于文件hello.txt保存时使用的字符集
  7.             isr = new InputStreamReader(fis,"UTF-8");
  8.             char[] cbuf = new char[20];
  9.             int len;
  10.             while ((len = isr.read(cbuf)) != -1){
  11.                 String str = new String(cbuf,0,len);
  12.                 System.out.print(str);
  13.             }
  14.         } catch (IOException e) {
  15.             throw new RuntimeException(e);
  16.         } finally {
  17.             try {
  18.                 isr.close();
  19.             } catch (IOException e) {
  20.                 throw new RuntimeException(e);
  21.             }
  22.         }
  23.     }
复制代码
文件复制–转换流

  1. @Test
  2.     public void test1() throws IOException {
  3.         File file1 = new File("hello.txt");
  4.         File file2 = new File("hello_gbk.txt");
  5.         FileInputStream fis = new FileInputStream(file1);
  6.         FileOutputStream fos = new FileOutputStream(file2);
  7.         InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
  8.         OutputStreamWriter osw = new OutputStreamWriter(fos,"GBK");
  9.         char[] cbuf = new char[20];
  10.         int len;
  11.         while ((len = isr.read(cbuf)) != -1){
  12.             osw.write(cbuf,0,len);
  13.         }
  14.         isr.close();
  15.         osw.close();
  16.     }
复制代码
2、其他流的利用

   1、标准的输入、输出流
2、打印流
3、数据流:用于读取或写出基本数据类型的变量或字符串
4、网络编程概述
  输入流测试

   键盘输入字符串,要求读取的整行字符串转成大写输出,然后继续进行输入操作。直至输入"e"或"exit"时,退出程序
  1.     public static void main(String[] args) {
  2.         BufferedReader br = null;
  3.         try {
  4.             InputStreamReader isr = new InputStreamReader(System.in);
  5.             br = new BufferedReader(isr);
  6.             while(true){
  7.                 System.out.println("输入:");
  8.                 String data = br.readLine();
  9.                 if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
  10.                     System.out.println("程序结束");
  11.                     break;
  12.                 }
  13.                 String upperCase = data.toUpperCase();
  14.                 System.out.println(upperCase);
  15.             }
  16.         } catch (IOException e) {
  17.             throw new RuntimeException(e);
  18.         } finally {
  19.             try {
  20.                 br.close();
  21.             } catch (IOException e) {
  22.                 throw new RuntimeException(e);
  23.             }
  24.         }
  25.     }
复制代码
打印流测试

  1.     @Test
  2.     public void test1() {
  3.         PrintStream ps = null;
  4.         try {
  5.             FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
  6. // 创建打印输出流,设置为自动刷新模式(写入换行符或字节 '\n' 时都会刷新输出缓冲区)
  7.             ps = new PrintStream(fos, true);
  8.             if (ps != null) {// 把标准输出流(控制台输出)改成文件
  9.                 System.setOut(ps);
  10.             }
  11.             for (int i = 0; i <= 255; i++) { // 输出ASCII字符
  12.                 System.out.print((char) i);
  13.                 if (i % 50 == 0) { // 每50个数据一行
  14.                     System.out.println(); // 换行
  15.                 }
  16.             }
  17.         } catch (FileNotFoundException e) {
  18.             e.printStackTrace();
  19.         } finally {
  20.             if (ps != null) {
  21.                 ps.close();
  22.             }
  23.         }
  24.     }
复制代码
数据流测试

  1.     @Test
  2.     public void test1() throws IOException {
  3.         DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
  4.         dos.writeUTF("AAA");
  5.         dos.flush();
  6.         dos.writeInt(123);
  7.         dos.flush();
  8.         dos.writeBoolean(true);
  9.         dos.flush();
  10.         dos.close();
  11.     }
  12.     @Test
  13.     public void test2() throws IOException {
  14.         DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
  15.         String name = dis.readUTF();
  16.         int i = dis.readInt();
  17.         boolean b = dis.readBoolean();
  18.         System.out.println("name=" + name);
  19.         System.out.println("i=" + i);
  20.         System.out.println("b=" + b);
  21.     }
复制代码
3、对象流

   用于存储和读取基本数据类型或对象的处理流,可以把Java中的对象写入到数据流中,也能把对象从数据源中还原
1、序列化:将内存中的java对象生存到磁盘中或通过网络传输出去
2、反序列化:将磁盘文件中的对象还原为内存中的一个java对象
3、可序列化类条件:
①必要实现接口Serializable
②当前类提供一个全局常量:serialVersionUID
③当前类内部所有属性也必须是可序列化的
增补:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
  可序列化类展示

  1. public class Person implements Serializable {
  2.     public final long serialVersionUID = 432142132L;
  3.     private String name;
  4.     private int age;
  5.     public Person(String name, int age) {
  6.         this.name = name;
  7.         this.age = age;
  8.     }
  9.     public String getName() {
  10.         return name;
  11.     }
  12.     public void setName(String name) {
  13.         this.name = name;
  14.     }
  15.     public int getAge() {
  16.         return age;
  17.     }
  18.     public void setAge(int age) {
  19.         this.age = age;
  20.     }
  21.     @Override
  22.     public String toString() {
  23.         return "Person{" +
  24.                 "name='" + name + '\'' +
  25.                 ", age=" + age +
  26.                 '}';
  27.     }
  28. }
复制代码
3、随机存储文件流


写进文件测试

  1.     @Test
  2.     public void test1() {
  3.         RandomAccessFile raf1 = null;
  4.         RandomAccessFile raf2 = null;
  5.         try {
  6.             raf1 = new RandomAccessFile(new File("hello.txt"),"r");
  7.             raf2 = new RandomAccessFile(new File("world.txt"),"rw");
  8.             byte[] buffer = new byte[1024];
  9.             int len;
  10.             while ((len = raf1.read(buffer))!= -1){
  11.                 raf2.write(buffer,0,len);
  12.             }
  13.         } catch (IOException e) {
  14.             throw new RuntimeException(e);
  15.         } finally {
  16.             if (raf1 !=null){
  17.                 try {
  18.                     raf1.close();
  19.                 } catch (IOException e) {
  20.                     throw new RuntimeException(e);
  21.                 }
  22.             }
  23.             if (raf2 != null){
  24.                 try {
  25.                     raf2.close();
  26.                 } catch (IOException e) {
  27.                     throw new RuntimeException(e);
  28.                 }
  29.             }
  30.         }
  31.     }
复制代码
数据插入结果实现

  1.   @Test
  2.     public void test1() throws IOException {
  3.         RandomAccessFile raf = new RandomAccessFile(new File("hello.txt"),"rw");
  4.         raf.seek(3);//将指针调到角标为3的位置
  5.         //保存指针3后面的所有数据到StringBuilder中
  6.         StringBuilder sb = new StringBuilder((int)new File("hello.txt").length());
  7.         byte[]buffer = new byte[20];
  8.         int len;
  9.         while ((len = raf.read(buffer))!= -1){
  10.             sb.append(new String(buffer,0,len));
  11.         }
  12.         //调回指针,写入xyz
  13.         raf.seek(3);
  14.         raf.write("xyz".getBytes());
  15.         //将StringBuider中的数据写入到文件中
  16.         raf.write(sb.toString().getBytes());
  17.         raf.close();
  18.     }
复制代码
4、网络编程概述

IP与端标语

    IP:唯一的标识 Internet上的计算机(通讯实体)
在Java中利用InetAddress类代表IP
IP分类:IPv4 和 IPv6; 万维网 和 局域网
  域名: www.hao123.com
本地回路地址: 127.0.0.1 对应着localhost
怎样实例化InetAddress:两个方法:getByName(String host)、getLocalHost()
  1. public static void main(String[] args) {
  2.         try {
  3.             InetAddress inet1 = InetAddress.getByName("192.168.10.15");
  4.             System.out.println(inet1);///192.168.10.15
  5.             InetAddress inet2 = InetAddress.getByName("www.hao123.com");
  6.             System.out.println(inet2);//www.hao123.com/153.37.235.50
  7.             InetAddress inet3 = InetAddress.getByName("127.0.0.1");
  8.             System.out.println(inet3);///127.0.0.1
  9.             //获取本地地址方式2
  10.             InetAddress inet4 = InetAddress.getLocalHost();
  11.             System.out.println(inet4);
  12.             //getHostName()
  13.             System.out.println(inet2.getHostName());//www.hao123.com
  14.             //getHostAddress()
  15.             System.out.println(inet2.getHostAddress());//153.37.235.50
  16.         } catch (UnknownHostException e) {
  17.             throw new RuntimeException(e);
  18.         }
  19.     }
复制代码
  端标语:正在计算机上运行的历程
要求:差别的历程有差别的端标语
范围:被规定为一个16位的整数,0~65535
端标语和IP地址的组合得出一个网络套接字:Socket
  网络通讯


TCP

  1.     @Test
  2.     public void  client()  {
  3.         Socket socket = null;
  4.         OutputStream os = null;
  5.         try {
  6.             //创建Socket对象,指明IP与端口号
  7.             InetAddress inet = InetAddress.getByName("127.0.0.1");
  8.             socket = new Socket(inet,8899);
  9.             //获取一个输出流,用于输出数据
  10.             os = socket.getOutputStream();
  11.             //写出数据的操作
  12.             os.write("我是客户端".getBytes());
  13.         } catch (IOException e) {
  14.             throw new RuntimeException(e);
  15.         } finally {
  16.             if (os != null){
  17.                 try {
  18.                     os.close();
  19.                 } catch (IOException e) {
  20.                     throw new RuntimeException(e);
  21.                 }
  22.             }
  23.             if (socket !=null){
  24.                 try {
  25.                     socket.close();
  26.                 } catch (IOException e) {
  27.                     throw new RuntimeException(e);
  28.                 }
  29.             }
  30.         }
  31.     }
  32.     @Test
  33.     public void server(){
  34.         ServerSocket ss = null;
  35.         Socket socket = null;
  36.         InputStream is = null;
  37.         ByteArrayOutputStream baos = null;
  38.         try {
  39.             //创建服务器端的ServerSocket,指明自己的端口号
  40.             ss = new ServerSocket(8899);
  41.             //调用accept()表示接收来自客户端的socket
  42.             socket = ss.accept();
  43.             //获取输入流
  44.             is = socket.getInputStream();
  45.             //读数据
  46.             baos = new ByteArrayOutputStream();
  47.             byte[] buffer = new byte[5];
  48.             int len;
  49.             while ((len = is.read(buffer)) != -1){
  50.                baos.write(buffer,0,len);
  51.             }
  52.             System.out.println(baos.toString());
  53.         } catch (IOException e) {
  54.             throw new RuntimeException(e);
  55.         } finally {
  56.             if (baos !=null){
  57.                 try {
  58.                     baos.close();
  59.                 } catch (IOException e) {
  60.                     throw new RuntimeException(e);
  61.                 }
  62.             }
  63.             if (is !=null){
  64.                 try {
  65.                     is.close();
  66.                 } catch (IOException e) {
  67.                     throw new RuntimeException(e);
  68.                 }
  69.             }
  70.             if (socket!=null){
  71.                 try {
  72.                     socket.close();
  73.                 } catch (IOException e) {
  74.                     throw new RuntimeException(e);
  75.                 }
  76.             }
  77.             if (ss !=null){
  78.                 try {
  79.                     ss.close();
  80.                 } catch (IOException e) {
  81.                     throw new RuntimeException(e);
  82.                 }
  83.             }
  84.         }
  85.     }
复制代码
UDP

  1.     @Test
  2.     public void  sender() throws IOException {
  3.         DatagramSocket socket = new DatagramSocket();
  4.         String str = "我是UDP方式发送的数据";
  5.         byte[] data = str.getBytes();
  6.         InetAddress inet = InetAddress.getLocalHost();
  7.         DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
  8.         socket.send(packet);
  9.         socket.close();
  10.     }
  11.     @Test
  12.     public void receiver() throws IOException {
  13.         DatagramSocket socket = new DatagramSocket(9090);
  14.         byte[] buffer = new byte[100];
  15.         DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
  16.         socket.receive(packet);
  17.         System.out.println(new String(packet.getData(),0,packet.getLength()));
  18.         
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

灌篮少年

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表