关于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();}}
阅读全文
1 0
- 关于JUnit 的笔记
- 关于junit的疑问
- JUNIT的用法笔记
- 关于Junit注释的说明
- Junit的学习笔记一
- 关于junit
- 关于Junit
- Junit学习笔记之一:junit的安装和测试原则
- 关于一个junit的例子的分析
- JUnit笔记
- Junit笔记
- Junit笔记
- JUnit关于数据库访问的单元测试 (2)
- JUnit关于数据库访问的单元测试 (fi)
- 关于Eclipse插件的JUnit测试。
- 关于JUNIT的多线程测试问题
- Junit 使用中,关于SessionFactory的建立
- 第一篇关于 junit 的测试
- 令牌的生成和验证工具类
- PAT (Basic Level) Practise (中文) 1036. 跟奥巴马一起编程(15)
- 数据库的范式(1NF、2NF、3NF、BNCF)
- Android系统目录结构
- node.js后台快速搭建在阿里云(二)(pm2和nginx篇)
- 关于JUnit 的笔记
- Golden.Software.Grapher.v12.7.855.Win32_64 1CD
- 虚拟机中除去虚拟网桥virbr0
- Shanghai 2006,UVaLive(LA) 3695 Distant Galaxy题解
- openlayers3 气泡框展示鼠标点击坐标
- android相关视频下载地址
- hdu 1024 Max Sum Plus Plus 最大m个子序列
- ZYNQ学习笔记(一): uboot 编译
- Ubuntu Tomcat服务器设置自动重启,定时检查崩溃后重启