JUnit,IO流

JAVA(JUnit,IO流)

一,JUnit单元测试

java语言单元测试框架

可以书写一些列的测试方法,对项目所有的接口或方法进行单元测试

启动后,自动化的测试

只需查看最后的结果

每个单元测试的用例相对独立,由JUnit启动

添加,删除,屏蔽测试方法

 

jar包

要引入第三方的插件

 

//Stream

 

Test注解

      1.Test
      2.Before,在测试方法执行之前执行的方法
      3.After
     
      命名规范
      单元测试类的命名,被测试类的类名 + Test
      测试方法的命名:test + 被测试方法的方法名

//test注解时JUnit提供的一个单元测试注解
    //需要导包

    /**
     * 测试方法不能有返回值
     * 不能有参数
     */
    @Test
    public void test1(){
        System.out.println("heollo1");
    }

    @Test
    public void test2(){
        System.out.println("heollo2");
    }

 

JUnit断言

JUnit所有的断言都包含在Assert类中
这个类提供了很多有用的断言来编写测试用例
只有失败的断言才会被记录
     
      1.assertEquals :检查两个变量或等式是否相等
      2.assertTrue:检查条件是否为真
      3.assertFalse:检查条件是否为假
      4.assertNotNull:插件对象是否不为空
      5.assertNull:检查对象是否为空
     

 断言不成功就会抛异常,即使程序正常运行,结果不正确,也会以失败结束

案例:Hashtable和ConcurrentHashMap性能测试


/**
 * 集合
 * 1.Hashtable和ConcurrentHashMap性能测试
 *
 */
public class Test3 {

    /**
     * 尝试50个线程,每个线程向集合中put 100000个元素
     * 测试2个类所需时间
     */
    @Test
    public void hashtableTest() throws InterruptedException {
        final Map<Integer,Integer> map = new Hashtable<>();
        //计数器
        final CountDownLatch countDownLatch = new CountDownLatch(50);
        System.out.println("开始测试");
        long start = System.currentTimeMillis();
        for (int i = 0; i < 50; i++) {
            final int j = i;
            new Thread( () ->{
                for (int k = 0; k < 100000; k++) {
                    map.put(j * k,1);
                }//记录添加的次数
                countDownLatch.countDown();
            }).start();
            countDownLatch.await();
            long end = System.currentTimeMillis();
            System.out.println("hashtable执行了" + (end - start));
        }
    }

    @Test
    public void hashMapTest() throws InterruptedException {
        final Map<Integer,Integer> map = new ConcurrentHashMap<>();
        //计数器
        final CountDownLatch countDownLatch = new CountDownLatch(50);
        System.out.println("开始测试");
        long start = System.currentTimeMillis();
        for (int i = 0; i < 50; i++) {
            final int j = i;
            new Thread( () ->{
                for (int k = 0; k < 100000; k++) {
                    map.put(j * k,1);
                }//记录添加的次数
                countDownLatch.countDown();
            }).start();
            countDownLatch.await();
            long end = System.currentTimeMillis();
            System.out.println("hashtable执行了" + (end - start));
        }
    }



}

 

JDK8新增


     Stream编程
     容器对象功能的增强
     
     使用一个流的时候,包括三个步骤
       1.获取一个数据源
       2.执行操作获取想要的结果
       3.每次操作,原有的流对象不改变,返回一个新的Stream对象
     
     Stream有几个特性:
       1.Stream不存储数据,一般会输出结果
       2.Stream不会改变数据源,通常生成一个新的集合
       3.stream具有延迟执行的特性,只有调用终端操作时,中间操作才会执行

 

案例:

 @Test
    public void test1(){
        List<String> list = Arrays.asList("a","b","c");
        //创建一个顺序流
        Stream<String> stream = list.stream();
        //创建一个并行流
        Stream<String> parallelStream = list.parallelStream();

        Stream<Integer> integerStream = Stream.of(1, 2, 3);

        Stream.iterate(0,(x) -> x+3);

        list.stream().forEach(System.out::println);


    }

 

二,Java IO流

Input:把数据从物理内存加载到运行内存。(读)

Output:把数据从运行内存写到物理内存。(写文件)

 

对于文件的操作

Java.io下的包

工具类:File操作文件的类

1.文件的路径

  正斜杠: /

  反斜杠: \

  Unix / Linux ,路径分割采用正斜杠 /

  在windows中,路径分割采用反斜杠 \

  在java中, \ 代表转义

  在File类中,定义了路径分隔符的常量,自动识别操作系统

    File.separator  斜杠

    File.pathSparator  ;

File类定义的常量

static String pathSeparator
与系统相关的路径分隔符字符,为方便起见,表示为字符串。
static char pathSeparatorChar
与系统相关的路径分隔符。
static String separator
与系统相关的默认名称 - 分隔符字符,以方便的方式表示为字符串。
static char separatorChar
与系统相关的默认名称分隔符。

2.File类的构造器

File(File parent, String child)
从父抽象路径名和子路径名字符串创建新的 File实例。
File(String pathname)
通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
File(String parent, String child)
从父路径名字符串和子路径名字符串创建新的 File实例。
File(URI uri)
通过将给定的 file: URI转换为抽象路径名来创建新的 File实例。

 

3.文件的操作

创建文件

File file = new File("e:\\aaa.txt");
        try {
            file.createNewFile();//文件新建不会覆盖原来的文件
            System.out.println("文件创建成功");
        } catch (IOException e) {
            e.printStackTrace();
        }

删除文件

//File类的删除不走回收站
    @Test
    public void test2(){
        File file = new File("e:\\aaa.txt");
        file.delete();
    }

 文件夹的创建

//文件夹
    @Test
    public void test3(){
        File file = new File("e:/a");
        boolean mkdir = file.mkdir();
        System.out.println(mkdir);
    }

//多级文件夹
        File file = new File("e:/a/b/c/d");
        boolean mkdir = file.mkdir();
        boolean mkdirs = file.mkdirs();//多级目录
        System.out.println(mkdirs);

文件夹的删除

        File file = new File("e:/a/b/c/d");
//        boolean mkdir = file.mkdir();
        boolean mkdirs = file.mkdirs();//多级目录
        System.out.println(mkdirs);
        boolean delete = file.delete();
        System.out.println(delete);

 创建文件和文件

//创建文件夹和文件
    @Test
    public void test4(){
        //新建某一文件下的某个文件,这个路径还不存在,需要封装工具类
        File file = new File("e:/a/b/ac.txt");
        System.out.println(file.getName());//ac.txt
        System.out.println(file.getParent());//e:\a\b
        f(file);
    }

    static void f(File file){
        String a = file.getParent();
        String b = file.getName();

        File file1 = new File(a);
        file1.mkdirs();

        File file2 = new File(a,b);
        try {
            file2.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

判断文件夹是否存在

    @Test
    public void test5(){
        File file = new File("e:/a/c.txt");
        System.out.println(file.exists());
    }

 

绝对路径和相对路径

绝对路径:以盘符开头的

相对路径:没有指定的盘符开头

 

判断是不是绝对路径

    @Test
    public void test5(){
        File file = new File("e:/a/c.txt");
        System.out.println(file.isAbsolute());
    }

 

案例:模拟cmd

查看代码
 package JUnitss;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class TestCmd {
    private static Scanner sc = new Scanner(System.in);
    private  File file;

    public TestCmd() {
        file = new File("C:\\Users\\Administrator");
    }


    public void pan(String next){
        switch (next){
            case "c:":
            case "d:":
            case "e:":
            case "C:":
            case "D:":
            case "E:":
                this.file = new File(next.toUpperCase() + "\\");
                break;
            default:
                System.out.println(next + "不是内部或外部命令,也不是可运行的程序" + "或批处理文件。");
                break;
        }
    }

    public  void run(){

        while (true){

            String name = file.getPath();
            System.out.print(name + " >");//  C:\

            String next = sc.next();
            if(next.length() == 2 && next.charAt(1) == ':'){
                pan(next);
            }else {
                switch (next){
                    case "help":
                        help();
                        break;
                    case "mkir":
                        System.out.print(file.getPath() + " >");
                        String mkir = sc.next();
                        boolean mkir1 = mkir(mkir);
                        if(mkir1 == false)
                            System.out.println("创建文件夹失败");
                        break;
                    case "cd"://删除
                        System.out.print(file.getPath() + " >");
                        String cd = sc.next();
                        boolean cd1 = cd(cd);
                        if(cd1 == false)
                            System.out.println(next + "删除失败");
                        break;
                    case "cnf":
                        System.out.print(file.getPath() + " >");
                        String newCnf = sc.next();
                        boolean cnf = cnf(newCnf);
                        if(cnf == false)
                            System.out.println("创建失败");
                        break;
                    case "dir":
                        dir();
                        break;
                    default:
                        System.out.println(next + "不是内部或外部命令,也不是可运行的程序" + "或批处理文件。");
                        break;
                }
            }
        }
    }
    public void help(){
        System.out.println("------help------");
        System.out.println("有关某个命令的详细信息,请键入 HELP 命令名");
        System.out.println("进入盘符\t\t c: C: d: D: e: E: ");
        System.out.println("mkir\t\t创建文件夹");
        System.out.println("cd\t\t删除");
        System.out.println("cnf\t\t创建文件");
        System.out.println("dir\t\t查看盘下的目录");
        System.out.println("-----------------");
    }

    public boolean mkir( String mkir){
        return new File(file,mkir).mkdir();
    }

    public boolean cd(String cd){
        File delFile = new File(this.file, cd);
        if (!delFile.exists())
            return false;
        return delFile.delete();
    }

    public boolean cnf(String cnf){
        try {
            return new File(file,cnf).createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public void dir(){
        File filedir = new File(file.getPath());
        System.out.println();
        String[] list = filedir.list();
//        System.out.println(Arrays.toString(list));
        System.out.print(file.getPath() + " >");
        System.out.println("--------------------------------");
        for (int i = 0; i < list.length; i++) {
            System.out.print("文件  " + (i+1) + "     ");
            System.out.print(list[i] + "\n");
        }
        System.out.println("              共计" + list.length);
        System.out.println("--------------------------------");
    }

    public static void main(String[] args) {
        TestCmd t = new TestCmd();
        t.run();

    }
}

 

posted @ 2022-08-08 21:29  一只神秘的猫  阅读(37)  评论(0)    收藏  举报