关于JUnit 的笔记

来源:互联网 发布:什么时候跑步最好知乎 编辑:程序博客网 时间:2024/06/06 00:35

         在java单元开发中的小程序函数,使用@Test 在函数中的开始的上一行。

当选中编写的函数时 可以直接测试一个单元函数,没有必要写一个主函数,这样就有比较好的测试力。



public void TestLength(){/* * File 对象 * File类封装文件操作常用方法 * file对象只是对象 * 是内存中的一块区域,并非实际的文件,可以通过该对象去了解文件的信息 * file.separator 等价于 斜杠 * windows路径斜杠:\ * linux路径斜杠:/ * file.separator会根据操作系统进行斜杠判定使用 * file.length()返回的是文件在硬盘上实际占用的字节数 *  */File file = new File("demo" + File.separator + "ghost.txt");System.out.println(file + "占用的字节量" +  file.length());/* * 英文在文件中占用1个字节 * 中文在文件中占用2个字节 */}/* * 文件的创建 * file.createNewFile() */@Testpublic void TestCreateFile() throws IOException{File file = new File("demo" + File.separator + "ghost2.txt");/* * file.exists() 检测文件在硬盘上是否存在 * 返回值:存在为true *       不存在返回false *        */if(!file.exists()){file.createNewFile();}}/* * 文件的删除 * file.delete() */@Testpublic void TestDeleteFile(){File file = new File("demo" + File.separator + "ghost2.txt");file.delete();}/* * 文件夹的创建 */@Testpublic void TestMkDir() {File file = new File("某老师合集");file.mkdir();}/* * 创建多级文件夹 * file.mkdirs() */@Testpublic void TestMkDirs(){File file = new File("学习资料" + File.separator + "视频资料" + File.separator + "XXX合集");file.mkdirs();}/* * 删除文件夹 * file.delete() * 该文件夹内不能有其他的文件或者文件夹 */@Testpublic void TestDeleteMkDirs(){File file = new File("demo");file.delete();}/* * 文件的读写 * FileInputStream类 * 文件字节输入流 * 从文件中读入数据 *  * InputStream类 * 所有字节输入流的父类 * 输出是相对于程序而言的 * 从程序外读入数据为输入流 * 由于InputStream读入的都是字节数据 * 所以称之为字节输入流 * * FileOutputStream类 * 文件字节输出流 * 从程序中写出字节数据到文件 *  * OutputStream类 * 所有字节输出流的父类 * 输出是相对于程序而言的 * 从程序内部向外写出字节数据 * 由于OutputStream输出的都是字节数据 * 所以称之为字节输出流 *  *  * 程序中只有字节流才是真正处理业务的IO流(低级流) * 其他的io流都是包装流(高级流) */@Testpublic void TestFOS() throws IOException{/* * 覆盖掉源文件中的数据 */FileOutputStream fos = new FileOutputStream("demo" + File.separator + "ghost.txt");fos.write("7点半早点会寝室,别再外面浪".getBytes());fos.close();}@Testpublic void TestFOSByAppend() throws IOException {/* * 在文件中原始数据后进行追加数据 */FileOutputStream fos = new FileOutputStream("demo" + File.separator + "ghost.txt",true);fos.write("猥琐发育,不要浪".getBytes());fos.close();}/* * 从文件中读取数据 */@Testpublic void TestFis() throws IOException {/* * read() * 读取一个字节 * 返回实际读到字节 * 如果返回-1 * 则表示读到文件末尾 */FileInputStream fis = new FileInputStream("demo" + File.separator + "ghost.txt");int d = -1; while ((d = fis.read()) != -1) {System.out.print((char)d);} fis.close();}/* * 使用文件字节IO流 * 实现文件的复制 */@Test public void FileCopy() throws IOException{FileInputStream fis = new FileInputStream("demo" + File.separator + "ghost.txt");FileOutputStream fos = new FileOutputStream("demo" + File.separator + "ghost2.txt");int d = -1;while((d = fis.read()) != -1){fos.write(d);}System.out.println("复制完毕");fis.close();fos.close();}}

原创粉丝点击