Java字节流操作

package io;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * IO流概述和分类
 * IO流概述:
 * 1、IO:输入/输出(Input/Output)
 * 2、流:是一种抽象的概念,是对数据传输的总称,也就是说数据在设备间的传输称为流,流的本质是数据传输
 * 3、IO流就是用来处理设备间数据传输问题的(如:文件上传、下载、复制等操作)
 *
 * IO流分类:
 * 1、按照数据的流向分为:
 *      输入流:读数据
 *      输出流:写数据
 * 2、按照数据类型来分:
 *      字节流:
 *      字节输入流、字节输出流
 *      字符流:
 *      字符输入流、字符输出流
 *
 *      NOTE:
 *      1、可以通过普通编辑器打开并编辑的文件就是字符流(如txt文件)
 *      2、图像、视频等无法用普通编辑器编辑的文件就是字节流
 *      3、如果不知道具体属于什么数据类型,就用字节流
 * 3、字节流写入数据:
 *      字节流抽象基类:
 *      1、InputStream:这个抽象类是表示字节输入流的所有类的超类
 *      2、OutputStream:这个抽象类是表示字节输出流的所有类的超类
 *      3、字节流抽象类子类的特点:子类名称都是以其父类名作为子类名的后缀(如:FileInputStream)
 *      FileOutputStream:
 *      1、创建文件输出流以指定的名称写入文件
 * 4、字节缓冲流:
 *      字节缓存流:
 *      BufferOutptStream:该类实现缓冲输出流,通过设置,应用可以向底层输出流写入字节,然后一次调用即可输出流的内容
 *      BufferedIlputStream:该类将创建一个内部缓冲区数组,当从流中读取或跳过字节时,内部缓冲区将根据所包含的输入流中从新填充,一次很多字节
 *
 */

public class IoDemo {

    public static void main(String[] args) throws IOException {
        /*byteBaseWrite();
        byteBaseRead();
        copyBase();
        copyImg();*/

//        buffDemo();
        copyVideo();
    }

    /**
     * 字节流的写入基本操作
     * @throws IOException
     */
    public static void byteBaseWrite() {

        FileOutputStream fi = null;
        try {
            //构造方法需要指定一个资源路径
            boolean append = true;//如果指定第二个参数为true 则在原文件后面追加内容
            fi = new FileOutputStream("P:\\demo\\ddddd.txt",append);

            //写入单个数据 注意:参数是 int 类型
            fi.write(97);//注意:这是字节操作,因此97对应的是字符的a,所以文件中被写入了一个a

            //换行
            fi.write("\r\n".getBytes(StandardCharsets.UTF_8));

            //写入多个数据 注意:参数是byte数组
            byte[] bytes = "我喜欢Java".getBytes(StandardCharsets.UTF_8);//将字符串转换为byte字节
            fi.write(bytes);//我喜欢Java


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //操作完后一定要关闭资源
            if (fi != null) {
                try {
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 字节流的读取基本操作
     */
    public static void byteBaseRead() {
        FileInputStream fi = null;
        try {
            fi = new FileInputStream("P:\\demo\\ddddd.txt");
            //一次读取一个字节
            /*int read;
            while ((read = fi.read()) != -1){
                System.out.print(read);
            }*/

            //一次读取一个字节数组
            byte[] bys = new byte[5];//字母和数字:一个字节, 汉字:GBK 2个字节 UTF8 3个字节
            int len = fi.read(bys);//从该输入流读取最多 b.length 个字节的数据到一个字节数组

            System.out.println(new String(bys,0,len));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 复制文件:
     *      其实就是将一个文件的数据复制到另一个文件
     *
     *      FileInputStream:打开文件,读取其中数据
     *      FileOutputStream:创建新文件,并将上面读取到的数据写入
     */
    public static void copyBase() {

        FileInputStream fi = null;
        FileOutputStream fi1 = null;
        try {
            //打开文件
            fi = new FileInputStream("P:\\demo\\ddddd.txt");

            //创建文件
            fi1 = new FileOutputStream("P:\\demo\\bbbbbbb.txt");

            //读取打开的文件内容,将内容写入到新文件
            int by;
            while ((by = fi.read()) != -1){
                fi1.write(by);
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //操作完后一定要关闭资源
            if (fi != null) {
                try {
                    fi.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fi1 != null) {
                try {
                    fi1.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * 复制图片
     */
    public static void copyImg() {
        //打开文件
        FileInputStream fi = null;
        //写入文件
        FileOutputStream fos = null;
        try {
            //打开文件
            fi = new FileInputStream("C:\\Users\\admin\\Pictures\\IntelliJ IDEA 构建Java项目06.png");
            //写入文件
            fos = new FileOutputStream("C:\\Users\\admin\\Pictures\\ttttt.png");

            //读取/写入文件 单个字节
            int len;
            while ((len = fi.read()) != -1) {
                fos.write(len);
            }

            //读取/写入文件 字节数组
            byte[] bys = new byte[1024];
            int lens;
            while ((lens = fi.read(bys)) != -1) {
                fos.write(bys,0,lens);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 字节缓冲输出流和输入流
     */
    public static void buffDemo() {

        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try{
            //创建缓冲输出流
            bos = new BufferedOutputStream(new FileOutputStream("P:\\demo\\aaaaa.txt"));
            //写入数据
            bos.write("我喜欢Java,还喜欢Linux".getBytes(StandardCharsets.UTF_8));
            //释放资源
            bos.close();

            //创建缓冲输入流
            bis = new BufferedInputStream(new FileInputStream("P:\\demo\\aaaaa.txt"));
            //读取数据
            byte[] bys = new byte[1024];
            int len;
            while ((len = bis.read(bys)) != -1) {
                System.out.println(new String(bys,0,len));
            }

            //释放资源
            bis.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    /**
     * 复制视频
     */
    public static void copyVideo() {
        BufferedInputStream fis = null;
        BufferedOutputStream fos = null;

        try{
            //打开文件
            fis = new BufferedInputStream(new FileInputStream("C:\\Users\\admin\\Videos\\video_2020-12-18_17-34-09.mp4"));
            //准备写入的文件
            fos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\admin\\Videos\\video.mp4"));

            //读取文件/写入文件
            byte[] bys = new byte[1024];
            int len;
            while ((len = fis.read(bys)) != -1) {
                System.out.println(len);
                fos.write(bys,0,len);
            }

            fos.close();
            fis.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

Java利用集合完成斗地主的发牌和洗牌

Demo

package jihe;


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;

/**
 * 斗地主
 */
public class Landlords {
    public static void main(String[] args) {

        method1();
        System.out.println("------------------------------");
        method2();
    }

    /**
     * 斗地主
     * 需求:
     * 通过程序实现斗地主过程中的洗牌、发牌和看牌
     * 思路:
     * 1、创建牌盒,也就是定义一个集合对象
     * 2、往牌盒装牌
     * 3、洗牌,将牌中的顺序打乱 (用 Collections 的 shuffle 方法)
     * 4、发牌,也就是遍历集合,给三个玩家发牌
     * 5、看牌,输出三个玩家分配的牌
     */
    public static void method1() {
        //创建牌盒,也就是定义一个集合对象,用ArrayList集合实现
        ArrayList<String> array = new ArrayList<>();

        //往牌盒装牌
        //先定义花色数组
        String[] colors = {"♦", "♣", "♥", "♠"};
        //定义牌面数组
        String[] number = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};

        //拼接花色和牌面
        for (String c : colors) {
            for (String n : number) {
                //往牌盒中装牌
                array.add(c + n);
            }
        }

        //再拼接上小王、大王
        array.add("小王");
        array.add("大王");

        //洗牌 - 使用Collections
        Collections.shuffle(array);

        //创建三个玩家 和 三张底牌盒子
        ArrayList<String> a = new ArrayList<String>();
        ArrayList<String> b = new ArrayList<String>();
        ArrayList<String> c = new ArrayList<String>();
        ArrayList<String> div = new ArrayList<String>();

        //发牌
        for (int i = 0; i < array.size(); i++) {
            //拿到每一张牌
            String poker = array.get(i);
            if (i >= array.size() - 3) {
                //保留3张底牌
                div.add(poker);
            } else if (i % 3 == 0) {
                //给第一个玩家发牌
                a.add(poker);
            } else if (i % 3 == 1) {
                //给第二个玩家发牌
                b.add(poker);
            } else if (i % 3 == 2) {
                //给第三个玩家发牌
                c.add(poker);
            }
        }

        //看牌
        System.out.println("第一个人的牌是:" + a);
        System.out.println("第二个人的牌是:" + b);
        System.out.println("第三个人的牌是:" + c);
        System.out.println("三张底牌是:" + div);
    }


    /**
     * 需求:
     * 通过程序实现斗地主过程中的洗牌、发牌和看牌
     * 思路:
     * 1、创建HashMap 键是编号,值是牌
     * 2、创建ArrayList 存储编号
     * 3、创建 花色和点数 数组
     * 4、从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号
     * 5、洗牌(洗的是编号,为了保证编号是排序的,创建TreeSet集合接收)
     * 7、看牌(遍历TreeSet集合,获取编号,到HashMap中查找对应的牌)
     */
    public static void method2() {
        //创建HashMap 键是编号 值是牌
        HashMap<Integer, String> hm = new HashMap<>();

        //创建ArrayLIst 存储编号
        ArrayList<Integer> array = new ArrayList<>();

        //创建花色数组
        String[] colors = {"♦", "♣", "♥", "♠"};
        //定义牌面数组
        String[] number = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};

        //从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号
        int index = 0;
        for (String n : number) {
            for (String c : colors) {
                hm.put(index, c + n);
                array.add(index);
                index++;
            }
        }

        //大小王
        hm.put(index, "小王");
        array.add(index);
        index++;
        hm.put(index, "大王");
        array.add(index);

        //洗牌
        Collections.shuffle(array);
        //定义角色和保留三张
        TreeSet<Integer> a = new TreeSet<>();
        TreeSet<Integer> b = new TreeSet<>();
        TreeSet<Integer> c = new TreeSet<>();
        TreeSet<Integer> div = new TreeSet<>();

        for (int i = 0; i < array.size(); i++) {
            //拿到每一张牌
            int x = array.get(i);
            if (i >= array.size() - 3) {
                //保留3张底牌
                div.add(x);
            } else if (i % 3 == 0) {
                //给第一个玩家发牌
                a.add(x);
            } else if (i % 3 == 1) {
                //给第二个玩家发牌
                b.add(x);
            } else if (i % 3 == 2) {
                //给第三个玩家发牌
                c.add(x);
            }
        }

        //看牌
        lookPoker("a", a, hm);
        lookPoker("b", b, hm);
        lookPoker("c", c, hm);

        //底牌
        System.out.print("底  牌 是:");
        for (Integer dv : div) {
            System.out.print(hm.get(dv) + " ");
        }
    }

    public static void lookPoker(String name, TreeSet<Integer> ts, HashMap<Integer, String> hm) {
        System.out.print(name + "的牌面是:");
        for (Integer key : ts) {
            String poker = hm.get(key);
            System.out.print(poker + " ");
        }
        System.out.println();
    }
}

Java利用集合完成斗地主发牌、洗牌.png

Java中Set集合的两种排序

Demo:

package jihe;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * set 集合中的 TreeSet集合
 * 
 * 需求:
 * 使用该集合存储多个学生的考试成绩信息(姓名、学号、语文成绩、数学成绩、历史成绩)
 * 
 * 要求:
 * 按照总分降序排序
 */
public class TreeSetDemo {
    public static void main(String[] args) {
        Student s1 = new Student("张三", 95, 88, 76);
        Student s2 = new Student("李四", 67, 93, 84);
        Student s3 = new Student("王五", 88, 99, 77);
        Student s4 = new Student("赵六", 63, 75, 99);
        Student s5 = new Student("田七", 71, 66, 98);

        //自然排序
        nature(s1, s2, s3, s4, s5);

        System.out.println("\n\r");
        
        //比较器排序
        comp(s1,s2,s3,s4,s5);
    }

    /**
     * 自然排序
     *
     * @param s1
     * @param s2
     * @param s3
     * @param s4
     * @param s5
     */
    public static void nature(Student s1, Student s2, Student s3, Student s4, Student s5) {
        TreeSet<Student> st = new TreeSet<>();
        st.add(s1);
        st.add(s2);
        st.add(s3);
        st.add(s4);
        st.add(s5);

        for (Student s : st) {
            System.out.println("姓名:" + s.getName() + ",语文分数:" + s.getLangen() + ",数学分数:" + s.getMath() + ",历史分数:" + s.getHistory() + ",总分成绩:" + s.getTotal());
        }
    }

    /**
     * 比较强排序
     *
     * @param s1
     * @param s2
     * @param s3
     * @param s4
     * @param s5
     */
    public static void comp(Student s1, Student s2, Student s3, Student s4, Student s5) {

        //匿名对象
        TreeSet<Student> st = new TreeSet<>(new Comparator<Student>() {
            /**
             * 重写compareTo方法 实现自定义排序
             * @param o1 上一个元素
             * @param o2 当前元素
             * @return int 0、正数、负数
             * 返回 0 表示元素已存在,不会存储
             * 返回 正数 表示元素插入后面
             * 返回 负数 表示元素插入前面
             */
            @Override
            public int compare(Student o1, Student o2) {
                //这里降序排序
                int i = o1.getTotal() > o2.getTotal() ? -1 : 1;
                return i;
            }
        });

        st.add(s1);
        st.add(s2);
        st.add(s3);
        st.add(s4);
        st.add(s5);

        for (Student s : st) {
            System.out.println("姓名:" + s.getName()+",语文分数:"+s.getLangen() + ",数学分数:"+s.getMath() + ",历史分数:"+s.getHistory()+",总分成绩:"+s.getTotal());
        }
    }

}

Student类:

package jihe;

/**
 * 实现排序功能需要实现 Comparable 接口,并且指定 泛型 为需要操作的 引用数据 类型
 */
public class Student implements Comparable<Student> {
    String name;//姓名
    int langen;//语文成绩
    int math;//数学成绩
    int history;//历史成绩

    public Student(String name, int langen, int math, int history) {
        this.name = name;
        this.langen = langen;
        this.math = math;
        this.history = history;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLangen() {
        return langen;
    }

    public void setLangen(int langen) {
        this.langen = langen;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }

    public int getHistory() {
        return history;
    }

    public void setHistory(int history) {
        this.history = history;
    }

    /**
     * 获取学生总分
     *
     * @return
     */
    public int getTotal() {
        return langen + math + history;
    }

    /**
     * 重写compareTo方法 实现自定义排序
     *
     * @param s 需要比较 的对象
     * @return int 0、正数、负数
     * 返回 0 表示元素已存在,不会存储
     * 返回 正数 表示元素插入后面
     * 返回 负数 表示元素插入前面
     */
    @Override
    public int compareTo(Student s) {
        //这里升序排序
        int i = this.getTotal() > s.getTotal() ? 1 : -1;
        return i;
    }
}

TreeSet的排序.png
NOTE:

  1. 由于Set集合的不保证排序方式,因此在Set集合的子类TreeSet类中提供了两种排序方式
  2. 自然排序和比较强排序
  3. 自然排序是在操作的对象中实现 Comparable 接口 (implements Comparable
  4. 比较器排序是在操作的对象实例化时提供构造参数 Comparable (implements Comparable

常见的数据结构

常见的数据结构:
栈:

  1. 数据进入栈模型的过程称为:进栈
  2. 数据取出栈模型的过程称为:出栈
  3. 栈就像是一个桶,只有顶部有出口
  4. 这种数据进出的模式 也就是: 先进后出(后进先出)

与栈对应的是队列

队列:

  1. 数据进入队列是从后端进入
  2. 数据取出队列是从前端离开
  3. 队列就像是一个管道,两端都有口子

数组:

  1. 数组是一种 查询快,增删慢的数据
  2. ArrayList底层数据结构就是数组

链表:

  1. 链表是一种 查询慢、增删快的数据(相比较于数组)
  2. LinkedList底层数据结构就是链表

ListIterator迭代器

package jihe;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * 列表迭代器
 * 1、该迭代器继承自 Iterator
 * 2、该迭代器允许倒序遍历
 * 3、可以插入数据
 */
public class ListiteraorDemo {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();

        list.add("hello");
        list.add("world");
        list.add("java");

        //列表迭代器
        ListIterator<String> it = list.listIterator();

        //正序遍历
        while (it.hasNext()){
            String s = it.next();

            //添加元素 - 在最后一个元素后面添加
            if (s.equals("java")) {
                it.add("php");
            }
        }

        System.out.println(list);//[hello, world, java, php]


        System.out.println("\r\n");


        //倒序遍历
        while (it.hasPrevious()){
            System.out.println(it.previous());
        }



    }
}

NOTE:

  1. 该迭代器继承自 Iterator
  2. 该迭代器允许倒序遍历
  3. 迭代器中可以插入数据

List集合与迭代器

package jihe;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * List集合:
 * 1、该集合是有序集合(也成为序列)
 * 2、用户可以精确控制列表中每个元素的插入位置,可以通过数组索引的方式访问元素
 * 3、与Set集合不同,该集合允许重复的元素
 */
public class ListDemo {
    public static void main(String[] args) {
        //创建集合对象 - 多态模式
        List<String> list = new ArrayList<>();
        list.add("第一个");
        list.add("第二个");
        list.add("第三个");
        list.add("第二个");//重复的元素

        System.out.println(list);//[第一个, 第二个, 第三个, 第二个]

        //获取集合中指定元素
        System.out.println(list.get(1));//第三个

        //在指定位置插入元素
        list.add(1,"我在哪里");//新插入的元素 将原位置及后面的元素全部往后位移+1
        System.out.println(list);//[第一个, 我在哪里, 第二个, 第三个, 第二个]

        //集合长度
        System.out.println(list.size());//5 元素长度

        //修改指定元素
        list.set(1,"我改变了");
        System.out.println(list);//[第一个, 我改变了, 第二个, 第三个, 第二个]

        //使用迭代器遍历
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());//输出下一个元素
        }

    }
}

Collection集合与迭代器

package jihe;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * 集合类之:Collection
 * 集合类的特点:
 * 提供一种存储空间可变的存储模型,存储的数据容量可以随时发生改变
 * Java中的集合分为:Collection(单列) 和 Map(双列)
 * Connection集合:
 * 1、该集合包含 List、 Set两种集合
 * 2、存储数据只有一列数据
 * 3、List可存储重复数据
 * 4、Set不可存储重复数据
 * 5、List 集合包含:ArrayList、LinkedList等集合
 * 6、Set 集合包含:HashSet、TreeSet 等集合
 *
 * Map集合:
 * 1、存储数据有两列
 * 2、存储数据不可重复
 * 3、Map包含:HashMap等集合
 */
public class CollectionDemo {

    public static void main(String[] args) {
        //使用多态调用ArrayList集合
        Collection<String> c = new ArrayList<String>();
        //添加 add 方法返回的永远是true
        c.add("第一个");
        c.add("第二个");
        c.add("第一个");//重复的元素

        System.out.println(c);//[第一个, 第二个, 第一个]

        //移除元素
        c.remove("第一个");//从左到右 移除
        System.out.println(c);//[第二个, 第一个]


        System.out.println(c.contains("第一个1"));//返回 boolen

        //清空集合
        //c.clear();
        System.out.println(c);//[]

        //判断 集合 是否存在
        System.out.println(c.isEmpty());//如果为空 则返回true 否则 false

        System.out.println(c.size());//集合中元素个数

        /**
         * 集合的遍历
         * iterator 迭代器
         * 每个集合中都包含了一个 iterator 迭代器
         */
        Iterator<String> it = c.iterator();
        //判断迭代器中的下一个元素是否存在
        while (it.hasNext()) {
            System.out.println(it.next());//输出集合的下一个元素 注意:拿出一个后指针向后移动一位
        }



    }
}

NOTE:

  1. Collection和Map集合
  2. 单列集合和双列集合
  3. 集合中的迭代器:iterator

Java异常捕获

Demo

package tryDemo;


import java.io.*;

public class Demo {
    public static void main(String[] args) {
        /**
         * 手动捕获异常必须用try .. catch 结构、
         * 并且catch中包含指定抛出的异常类
         */
        /*try {
            int method = method(5);
            System.out.println(method);
        }catch (ArrayIndexException e){
            System.out.println(e.getMessage());
        }*/

        method1();
    }

    //跑抛出异常 注意:这里是 throws
    public static int method(int index) throws ArrayIndexException
    {
        int[] ar = {5,9,3,7};
        //判断索引是否存在数组中
        if (index < ar.length){
            return ar[index];
        }else{
            //捕获一个异常,并且手动抛出 注意:这里是 throw
            throw new ArrayIndexException("指定的数组索引不存在");
        }

    }


    /**
     * 文件异常 处理
     * 文件的一些操作需要抛出IOException异常
     * 为了简化文件操作的异常处理,有新的处理方式
     */
    public static void method1()
    {
        //将资源定义在 try() 里面
        try(InputStreamReader isr = new InputStreamReader(new FileInputStream("P:\\demo\\bbbbbb.txt"));){
            //读取文件
            char[] chs = new char[1024];
            int i;
            while ((i = isr.read(chs)) != -1){
                System.out.println(new String(chs,0,i));
            }
        }catch (IOException e){
            System.out.println(e);
        }
    }
}

自定义异常类

package tryDemo;

/**
 * 自定义数组索引异常类
 * 异常类定义:
 * 必须继承 Exception 异常基类
 * 并且实现带参构造方法
 * 使用构造方法将异常信息传递到super类
 */
public class ArrayIndexException  extends Exception{
    public ArrayIndexException(){
    }

    //message 异常信息
    public ArrayIndexException(String message) {
        super("数组异常:" + message);
    }
}

自定义异常.png

Java时间日期转换

package date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo {
    public static void main(String[] args) {
        //获取当前时间的时间戳(默认毫秒)
        Date d1 = new Date(System.currentTimeMillis());
        System.out.println(d1);
        System.out.println(d1.getTime());


        /**
         * 时间格式化处理
         */
        //将时间戳转换成日期时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//用指定的格式 来格式化时间和日期
        Date d = new Date();//获取当前是时间戳
        System.out.println(sdf.format(d));//将时间戳转换为日期格式
        String ss = sdf.format(d);
        System.out.println(ss);//2020-12-15 16:09:36

        //将时间转换成时间戳
        String dt = "2019-08-09 11:11:11";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            long ts = sdf1.parse(dt).getTime() / 1000;//不知道为什么 parse 这个方法必须放在try结构中才不会报错 我的jdk是15.1
            System.out.println(ts);
            //再试着转回去
            System.out.println(sdf.format(ts * 1000));//2019-08-09 11:11:11

        } catch (ParseException e) {
            e.printStackTrace();
        }




    }
}

Java数据处理

  1. 时间戳
  2. 数组排序
  3. string 和 int 互转
package inter;

import java.lang.reflect.Array;
import java.util.Arrays;

public class Demo {
    public static void main(String[] args) {
        
        /**
         * Arrays 类 处理数组
         */
        //数组排序
        int[] arr = {12,58,36,54,11,45,74,61};
        System.out.println(Arrays.toString(arr));//[12, 58, 36, 54, 11, 45, 74, 61]
        Arrays.sort(arr);//排序
        System.out.println(Arrays.toString(arr));//[11, 12, 36, 45, 54, 58, 61, 74]


        /**
         * System 类 系统信息
         */
        //获取当前机器的毫秒时间戳
        long time = System.currentTimeMillis();
        System.out.println(time);//1608001816760
        //将毫秒时间戳转换为秒时间戳
        System.out.println(time / 1000);//1608001816

        //退出JVM虚拟机 status为负数表示异常退出
        //System.exit(-1);//进程已结束,退出代码-1

        /**
         * 基本数据类型的包装类
         * 基本数据类型       包装类
         * byte             Byte
         * shot             Short
         * int              Integer
         * long             Long
         * float            Float
         * double           Double
         * char             Characetr
         * boolean          Boolean
         */
        //例如:判断一个int的数据是否在int数据范围
        long i = 1545154586454L;
        System.out.println(i);//1545154586454
        System.out.println(Integer.MAX_VALUE);//2147483647
        System.out.println(Integer.MIN_VALUE);//-2147483648

        System.out.println((i > Integer.MAX_VALUE && i < Integer.MIN_VALUE) ? "该数据在int范围内" : "该数据不在int范围内");//该数据不在int范围内

        //将 string 转换为 integer 对象
        String s = "100";
        //System.out.println(Integer.valueOf("你好"));//报错 注意 该字符串必须是数字类型的字符串
        //方式一
        Integer s1 = Integer.valueOf(s);//先转换成 Integer 对象
        System.out.println(s1.intValue());//123456
        //方式二
        System.out.println(Integer.parseInt(s));//100

        //将 int 转换为 string 类型
        int number = 100;
        //方式一
        String s2 = "" + number;
        System.out.println(s2);//100
        //方式二
        System.out.println(String.valueOf(number));//100




    }
}