1. 常用的IO流有哪些?
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LeoitIU5-1597818906640)(upload/image-20200714150319630.png)]
2. 文件字节流
1. FileInputStream(文件字节输入流)
package com.uplooking.demo02;
import java.io.*;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
try {
//创建文件字节输入流
File file = new File("f:/up/a.txt");
FileInputStream fis = new FileInputStream(file);
//开始读取流中的字节数据,read()一次读取一个字节
//System.out.println(fis.read());
byte[] buf = new byte[(int) file.length()];
//读取数据到字节数组
fis.read(buf, 0, buf.length);
System.out.println(Arrays.toString(buf));
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
}finally{
fis.close;
}
}
}
上面的读取数据的写法写了两种一种是一个一个字节的读取数据,另外一种方法是吧数据读取到字节数组中;这两种方法都有弊端;
一个一个字节的读:读取过来的一个个的字节不方便保存和使用;
一次读取到字节数组:字节数组的长度有限的;
讲一种常用的读取方式:
package com.uplooking.demo03;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
try {
//创建文件字节输入流
File file = new File("f:/up/a.txt");
FileInputStream fis = new FileInputStream(file);
//开始读取流中的字节数据,read()一次读取一个字节,把读取到的字节保存下来
byte[] buf = new byte[1024];
//记录每次读取到的长度
int len = 0;
//用来追加每次读取到的值
StringBuffer sb = new StringBuffer();
while ((len = fis.read(buf)) != -1) {
//解码操作
String str = new String(buf, 0, len);
sb.append(str);
}
System.out.println(sb);
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
e.printStackTrace();
}
}
}
这种常用的读取方式是一个一个字节的读取,把读取的到的字节放进字节数组;每一次读取完成对值进行解码拼接;
2. FileOutputStream(文件字节输出流)
什么是文件字节输出流?
就是把内存(程序)中的数据,输出(写)到其他(文件)存储设备中
package demo01;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
//准备数据
String strData = "hello 中国";
FileOutputStream fos = null;
try {
//创建文件字节输出流(如果文件不存在则会创建新文件)
fos = new FileOutputStream("f:/up/aa.txt");
//开始写数据
fos.write(strData.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3. 文件移动
1.请输入源文件的路径
eg: f:/up/a.mp4
2.请输入移动的目标目录
eg: f:/up1/
3.移动完成之后
在: f:/up1/有一个a.mp4的文件,并且文件可以播放;
4. 文件大小的单位
1byte = 8bit(位)
1kb = 1024byte
1mb = 1024kb
1gb=1024mb
1tb=1024gb
1pb = 1024tb
eb
nb
.....
5. 移动文件
package demo02;
import java.io.*;
public class Main {
public static void main(String[] args) {
//1.读入源文件的路径 f:/up/a.mp4
File fromFile = new File("f:/up/a.mp4");
//2.读入目标目录 f:/up1
//通过字符串的截取,创建了一个目标文件
String targetFileName = fromFile.getPath().substring(fromFile.getPath().lastIndexOf(File.separator) + 1);
String toPath = "f:/up1/" + targetFileName;
//3.边读边写
byte[] buf = new byte[1024 * 1024];//1mb的缓冲区
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//3.1 创建文件字节输入流,把源文件的数据读到内存(程序)中
fis = new FileInputStream(fromFile);
fos = new FileOutputStream(toPath);
int len = 0;
//读到的长度只要不等于-1则证明没有读到文件的末尾,可以继续循环读取数据
while ((len = fis.read(buf)) != -1) {
// 3.2 创建文件字节输出流,把内存(程序)中的数据,写到目标文件(硬盘)
fos.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//释放资源(先打开的后关闭 后打开的先关闭)
if (fis != null && fos != null) {
fos.close();
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
//删除源文件
if (fromFile.delete()) {
System.out.println("移动文件成功!");
} else {
System.out.println("移动文件失败");
new File(toPath).delete();//把复制的目标文件删除
}
}
}
6. 文件字符流
6.1 文件字符输入流
字符流只适合读取纯文本文件
java.io.FileReader
package demo03;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
FileReader fr = null;
try {
fr = new FileReader("f:/up/a.txt");
//fr.read();读取数据返回读取到的字符
char[] buf = new char[1024];
//读取数据,返回读到的字符数
int len = 0;
StringBuffer sb = new StringBuffer();
while ((len = fr.read(buf)) != -1) {
sb.append(buf, 0, len);
}
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6.2 文件字符输出流
java.io.FileWriter
import java.io.FileWriter;
import java.io.IOException;
/**
* 文件字符输出流
*/
public class Demo04 {
public static void main(String[] args) {
FileWriter fw = null;
try {
//1. 创建文件字符输出流(FileWriter)
fw = new FileWriter("f:/up/bb.txt");
//2. 使用输出流把内存(程序)中的数据写到,硬盘(文件)中
fw.write("hello中国你好");
//fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.close();//关闭流的时候会自动刷出(flush)
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
7. 字节数组流
1. 字节数组输入流
ByteArrayInputStream是字节数组输入流,在内存中创建了一个字节数组,将输入流中读取的数据保存到字节数组的缓存区中.也就是说字节数组输入流将读取数据放到字节数组缓冲区中.
//定义源数据
byte[] sourseData = "hello中国".getBytes();
//创建字节数组输入流(用于把元数据(数组)读入到程序中)
ByteArrayInputStream bis = new ByteArrayInputStream(sourseData);
//targetDataArray:内存(程序)中存放数据的数组
byte[] targetDataArray = new byte[1024 * 1024];
int len = bis.read(targetDataArray);
System.out.println(new String(targetDataArray, 0, len));
bis.close();
2. 字节数组输出流
package demo06;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
//创建字节数组输出流(把内存中的数据写出到内存中(byteArray))
ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
//写数据
bos.write("hello".getBytes());
//把字节数组输出流中的数据拿出来放到字节数组并返回
System.out.println(new String(bos.toByteArray()));
bos.close();
}
}
8. 缓冲流
缓冲流是一种包装流,包装流就是用来增强基本的流的;
1. 缓冲字节输入流(BufferedInputStream)
package demo01;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
//创建基本流
FileInputStream fis = new FileInputStream("f:/up/a.txt");
//创建包装流(缓冲流),默认的缓冲区的大小为8192byte=8kb
BufferedInputStream bi = new BufferedInputStream(fis);
//创建接收数据的字节数组
byte[] buf = new byte[10];
//缓冲流开始读取数据
bi.read(buf);
System.out.println(Arrays.toString(buf));
//关闭资源,有包装流存在的时候,只需要关闭包装流,关闭的时候包装流会去先关闭其包装的基本流,再关闭自己
bi.close();
}
}
2. 缓冲字节输出流(BufferedOutputStream)
package demo02;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
//创建缓冲字节输出流
BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("f:/up/b.txt"));
bo.write("hello1 中国".getBytes());
bo.close();
}
}
3. 缓冲字符输入流(BufferedReader)
package demo03;
import java.io.BufferedReader;
import java.io.FileReader;
public class Main {
public static void main(String[] args) throws Exception {
//创建缓冲字符输入流
BufferedReader br = new BufferedReader(new FileReader("f:/up/a.txt"));
System.out.println(":::::::::" + br.readLine());//1
System.out.println(":::::::::" + br.readLine());//2
System.out.println(":::::::::" + br.readLine());//3
System.out.println(":::::::::" + br.readLine());//4
System.out.println(":::::::::" + br.readLine());//5
System.out.println(":::::::::" + br.readLine());//6
br.close();
}
}
4. 缓冲字符输出流(BufferedWriter)
package demo04;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) throws Exception {
//创建缓冲字符输出流
BufferedWriter br = new BufferedWriter(new FileWriter("f:/up/cc.txt"));
br.write("hello中国");
br.close();
}
}
9. 二进制流
二进制流也是包装流
二进制流可以保证数据的安全性;
1. 二进制(字节)输出流
package demo05;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
//创建二进制输出流
DataOutputStream dos = new DataOutputStream(new FileOutputStream("f:/up/dd.dat"));
//写一个字节
dos.write(97);
//只适合写英文字符串
//dos.writeBytes("中国");
//writeUtf既可以写中文也可以写英文
dos.writeUTF("中国");
dos.write(98);
dos.writeUTF("加油");
dos.write(99);
//关闭包装流
dos.close();
}
}
2. 二进制(字节)输入流
package demo06;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
//创建二进制字节输入流
DataInputStream dis = new DataInputStream(new FileInputStream("f:/up/dd.dat"));
//读入一个字节
System.out.println(dis.readByte());
System.out.println(dis.readUTF());
System.out.println(dis.readByte());
System.out.println(dis.readUTF());
System.out.println(dis.readByte());
}
}
10. 对象(字节)流
对象流可以用来读写对象
对象流也是包装流
0. 序列化的对象
package demo07;
import java.io.Serializable;
public class Person implements Serializable {
public static final long serialVersionUID = 1432432432L;//序列化ID
String name;
Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
}
1. 对象(字节)输出流
package demo07;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
//创建对象输出流
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f:/up/oo.obj"));
//创建对象
Person person = new Person();
person.name = "小花";
person.age = 20;
//给对象输出流写数据(对象),底层会进行序列化(对象--->字节数组)
oos.writeObject(person);
//释放资源
oos.close();
}
}
2. 对象(字节)输入流
package demo07;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class Main2 {
public static void main(String[] args) throws Exception {
//创建对象(字节)输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f:/up/oo.obj"));
//从对象输入流中读取对象 反序列化
Person person = (Person) ois.readObject();
System.out.println(person.name);
System.out.println(person.age);
//释放资源
ois.close();
}
}
11. 序列化(Serializable)
序列化:将对象转换为字节数组
12. 反序列化
反序列化: 将字节数组转换为对象
13. 主流的序列化与反序列化技术
- java中的自带的序列化技术
Serializable
反序列化时的id,必须和序列化的id一致- java中自带的序列化技术,给我们序列化出来的字节数组太大了;
- 主流的序列化和反序列化技术
- google的Protobuf(与平台无关)
- apache的avro
14. 随机读写文件流(RandomAccessFile)
随机读写文件流:
"随机"指的是我们可以使用seek(pos);来把内部的指针(游标Courser)移动到任意(pos)的位置;
随机读写文件流: 可以读数据到内存,也可以写数据到文件
package demo08;
import java.io.RandomAccessFile;
public class Main {
public static void main(String[] args) throws Exception {
//随机读写文件流 mode: r(只读模式) rw(读写模式) rws(读写执行模式)
RandomAccessFile randomAccessFile = new RandomAccessFile("f:/up/a.txt", "rw");
//读取一行内容
//System.out.println(randomAccessFile.readLine());
System.out.println(randomAccessFile.read());
//写一个字节,并且移动指针
randomAccessFile.write(98);//hbllo seek=1
//移动指针
randomAccessFile.seek(0);//seek=0
//写一个字节,并且移动指针
randomAccessFile.write(99);
System.out.println(randomAccessFile.read());
}
}
15. 软件测试
1. 软件测试是干啥的?
使用专业的手段,发现程序运行或者开发的不足;
2. 软件测试的分类
- 黑盒测试(大多数人都能干)
- 灰盒测试(稍微懂点代码)
- 白盒测试(懂代码)
- 自动化测试
- 单元测试(懂的比较多)
3. 单元测试(Junit)
Junit4
4. junit4的使用
下载jar包
得在项目中引用这个jar包:
5. junit4中的注解
@Before: 在每个测试用例执行之前都会执行
@Test: 注解一个测试用例
@After: 在每个测试用例执行之后执行