2022/08/08 吉软 Java基础(21) JUnit、流、IO流前言

一、JUnit单元测试

         JUnit是一个Java语言单元测试框架。

    @Test
    public void test01(){
        System.out.println("hello junit");
    }

    @Test
    public void test02() {
        System.out.println("hello junit02");
    }

1.1 JUnit单元测试的好处:

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

2.启动后,自动化的测试。

3.只需要查看最后的结果。

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

5.添加,删除,屏蔽测试方法。

使用单元测试需要导入:jar包

        (如果要引入第三方的插件,xxx.jar的文件)

        首先要把这个文件导入到我们的工程目录下

        其次,要添加到工程的依赖目录中

 1.2 JUnit断言

        JUnit的所有的断言都包含Assert类中,这个类提供了很多有用的断言来编写测试用例。

只有失败的断言才会被记录。

1.2.1 JUnit断言的常用方法:

  •  assertEquals:检查两个变量或等式是否平衡
  •  assertTrue:检查条件是否为真
  •  assertFalse:检查条件是否为假
  •  assertNotNull:检查对象是否不为空
  •  assertNull:检查对象是否为空

1.3 JUnit的三种注解:

  1.         1.Test
    1.         2.Before:在测试方法执行之前执行的方法
      1.         3.After

命名规范:

        单元测试类的命名:被测试类的类名 + Test

        测试方法的命名:test + 被测试方法的方法名

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

二、输入输出流原理

        在Java程序中,对于数据的输⼊和输出以“流”(stream)的⽅式进⾏;程序中通过指定的⽅法来输⼊和输出数据

 JDK8新增:Stream编程

容器对象功能的增强

        我们可以将流看做流水线,这个流水线是处理数据的流水线,当我们使用一个流的时候,通常包括三个步骤:

  • 获取一个数据源
  • 执行操作获取想要的结果
  • 每次操作,原有的流对象不改变,返回一个新的Stream对象

Stream有几个特性:

1.Stream不存储数据,一般会输出结果

2.Stream不会改变数据源,通常情况下会生成一个新的集合

3.Stream具有延迟执行的特性,只有调用终端操作时,中间操作才会执行。

JDK8函数式接口

Consumer<T>:消费者 void accept(T t)

Supplier:供应商 T get()

Function: R apply(T t),将一个数据转化成另一个数据

Predicate:断言,boolean test(T t),判断返回值是boolean

 三、字节流

 3.1、字节输入流FileInputStream

        程序以字节为单位从磁盘文件读取数据

/***************创建输⼊入流InputStream-begin********************/
FileInputStream in = null;
try {
    in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
} catch (FileNotFoundException e) {
    System.out.println("找不不到指定⽂文件");
    System.exit(0);
}
/***************创建输⼊入流InputStream-end********************/
long num = 0; //⽤用于计数的变量量,作⽤用是计算⼀一共读了了多少个字节
int b = 0; //⽤用于记录读取位置的变量量
try {
    /***************读取⽂文件-begin********************/
    while((b = in.read()) != -1){
        System.out.println((char)b);
        num ++;
    }
    in.close();
    System.out.println("共读取了了"+num+"个字节");
    /***************读取⽂文件-end********************/
} catch (IOException e) {
    System.out.println("⽂文件读取错误");
    System.out.println(0);
}

3.2、字节输出流FileOutputStream

         程序以字节为单位,向磁盘文件写入数据

/***************创建输出流FileOutputStream-begin********************/
FileInputStream in = null;
FileOutputStream out = null;
try {
    in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
    out = new FileOutputStream("C:\\Users\\qinzhicong\\Documents\\hello1.txt");
} catch (FileNotFoundException e) {
System.out.println("找不不到指定⽂文件");
System.exit(0);
}
/***************创建输出流FileOutputStream-end********************/
int b = 0;
try {
    /***************拷⻉贝⽂文件-begin********************/
    while((b = in.read()) != -1){
    out.write(b);
    }
    in.close();
    out.close();
    /***************拷⻉贝⽂文件-end********************/
} catch (IOException e) {
    System.out.println("⽂文件复制错误");
    System.exit(0);
}

 3.3 File的工具类:File操作文件的类

1.文件的路径

  •         正斜杠:左斜杠,撇,/
  •         反斜杠:右斜杠,捺,\

在Unix/Linux,路径的分隔采用正斜杠  /

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

  1. 在java中,\ 代表转义
  2. 在File类中,定义了路径分隔符的常量,自动识别操作系统。

3.4 File类的构造器

        // file就代表了当前的目录
        File file1 = new File("E:\\workspace\\idea");
        System.out.println("file1 = " + file1);
        File file2 = new File("E:\\workspace\\idea","aaa");
        System.out.println("file2 = " + file2);
        File file3 = new File(file1,"aaa");
        System.out.println("file3 = " + file3);

四、字符流

4.1、字符输入流FileReader

        程序以字符为单位从磁盘文件读取数据

FileReader fr = null;
try {
    fr = new FileReader("C:\\Users\\qinzhicong\\Documents\\hello.txt");
    int c = 0;
    while((c = fr.read()) != -1){
        System.out.println((char)c);
    }
    fr.close();
} catch (FileNotFoundException e) {
    System.out.println("找不不到指定⽂文件");
    System.exit(0);
} catch (IOException e) {
    System.out.println("⽂文件读取错误");
    System.exit(0);
}

4.2、字符输出流FileWriter

        程序以字符为单位,向磁盘文件写入数据

FileWriter fw = null;
try {
    fw = new FileWriter("C:\\Users\\qinzhicong\\Documents\\hello2.txt");
    String str = "HelloWorld我的Java世界";
    fw.write(str);
    fw.close();
} catch (IOException e) {
    System.out.println("⽂文件写⼊入错误");
    System.exit(0);
}

五、缓冲流

        缓冲流对读写的数据提供了缓冲的功能,提⾼了读写的效率

5.1、字节缓冲流

5.1.1、字节缓存输入流

try {
    FileInputStream in = new FileInputStream("C:\\Users\\qinzhicong\\Documents\\hello.txt");
    BufferedInputStream bis = new BufferedInputStream(in, 1024);
    int b;
    while((b = bis.read()) != -1){
        System.out.println((char)b);
    }
    bis.close();
    in.close();
} catch (FileNotFoundException e) {
    System.out.println("⽂文件没有找到");
} catch (IOException e) {
    System.out.println("⽂文件操作错误");
}

5.1.2、字节缓冲输出流

try {
    FileOutputStream out = new FileOutputStream("C:\\Users\\qinzhicong\\Documents\\hello1.txt");
    BufferedOutputStream bos = new BufferedOutputStream(out, 1024);
    bos.write("helloMyJava".getBytes());
    bos.close();
    out.close();
} catch (FileNotFoundException e) {
    System.out.println("⽂文件没有找到");
} catch (IOException e) {
    System.out.println("⽂文件操作错误");
}

5.2、字符缓冲流

5.2.1、字符缓冲输入流

try {
    FileReader fr = new FileReader("C:\\Users\\qinzhicong\\Documents\\hello.txt");
    BufferedReader br = new BufferedReader(fr, 1024);
    String str;
    while((str = br.readLine()) != null){
        System.out.println(str);
    }
    br.close();
    fr.close();
} catch (FileNotFoundException e) {
    System.out.println("⽂文件没有找到");
} catch (IOException e) {
    System.out.println("⽂文件操作错误");
}

5.2.2、字符缓冲输出流

try {
    FileWriter fw = new FileWriter("C:\\Users\\qinzhicong\\Documents\\hello2.txt");
    BufferedWriter br = new BufferedWriter(fw);
    br.write("来呀,相互伤害呀");
    br.newLine();
    br.write("来呀,相互伤害呀");
    br.close();
    fw.close();
} catch (FileNotFoundException e) {
    System.out.println("⽂文件没有找到");
} catch (IOException e) {
    System.out.println("⽂文件操作错误");
}

5.2.3、换行的方法

readLine:该⽅法是BufferedReader对象提供的⽅法,⽅法返回值是String类型,表⽰每次读取⼀⾏的内容

newLine:该⽅法是BufferedWriter对象提供的⽅法,表⽰向⽂件中写⼊⼀个换⾏符

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值