Junit单元测试
Junit
单元测试,即测试框架
将一个程序划分成单个类和单个方法,需要对这些方法进行测试看是否达到预期
Java中String类
在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串
String
类是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了
当对字符串进行修改的时候,需要使用 StringBuffer
和 StringBuilder
类
StringBuffer
和 StringBuilder
类的对象能够被多次的修改,并且不产生新的未使用对象
示例
package com.Lowell;
import org.junit.Test;
public class StringBuilderTest {
@Test
public void demo() {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Hello");
stringBuilder.append(",world!");
System.out.println(stringBuilder);
stringBuilder.reverse();
System.out.println(stringBuilder);
stringBuilder.append(" Hello,").append("Java!");
System.out.println(stringBuilder);
}
}
输出结果:
Hello,world! !dlrow,olleH !dlrow,olleH Hello,Java!
Java异常处理Exception
异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的。
try catch
捕获异常
- 定位一个代码块的异常,运行完成这个代码块后,抛出这个代码块的异常 示例
public class ExceptionTest {
public void demo() {
try{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}catch(Exception e){
System.out.println("Exception thrown : " + e);
}
System.out.println("Out of the block");
}
}
输出结果:
Exception thrown : java.lang.ArrayIndexOutOfBoundsException: 3 Out of the block
throw
/throws
关键字
throw
和throws
关键字是用于处理异常的。throw
用于在代码中抛出异常,而throws
用于在方法声明中指定可能会抛出的异常类型。 示例
public class ExceptionTest throws FileNotFoundException {
public void demo() {
if(1 != 0 ) {
throw new FileNotFoundException("File is not found.");
}
}
}
输出结果为:
java.io.FileNotFoundException:File is not found.
finally
关键字
- finally 关键字用来创建在 try 代码块后面执行的代码块。
- 无论是否发生异常,finally 代码块中的代码总会被执行。 语法如下:
try{
}catch(ExceptionType ExceptionName){
}finally{
}
Java文件类File
使用File
类new
一个对象时一定要使用绝对路径,这个类中不仅包含了对文件的操作,还有对文件夹的操作
示例
package com.Lowell;
import org.junit.Test;
import java.io.File;
public class FileTest {
@Test
public void demo() {
File file0 = new File("D:\\Project\\Idea_Java\\Java_test");
File file1 = new File("Project\\Idea_Java\\Java_test");
File file2 = new File("D:\\Project\\Idea_Java\\Java_test\\src\\com\\Lowell\\FileTest.java");
System.out.println(file0.getParentFile());
System.out.println(file0.isFile());
System.out.println(file0.equals(file1));
System.out.println(file1.equals(file2));
System.out.println(file0.length());
System.out.println(file1.isAbsolute());
System.out.println(file0.canExecute());
File dir = new File("D:\\Project\\Idea_Java\\Java_test\\src\\com\\Lowell");
String[] list = dir.list();
for (String name : list) {
System.out.println(name);
}
}
}
输出结果:
D:\Project\Idea_Java false false false 4096 false true ExceptionTest.java FileTest.java InterfaceTest.java overrideTest.java staticTest.java StringBuilderTest.java
Java I/O Stream
什么是
Stream
一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。
谁是输入,谁是输出,是一个相对而言的概念。都是数据源--->目标源
的过程。数据源可以是程序,文件,数据库
Reading information into a program 读数据,数据源数据流到了程序中,对于程序是input
,对于数据源是output
,是程序在读取数据源中的数据。
Writing information from a program 写数据,程序数据流到了数据源中,对于程序是output
,对于数据源是input
,是程序在往数据源中写数据。
InputStream和OutPutStream
I/O流就是用来管理各种数据的输入和输出,在这个包中有两个终极静态父类InputStream
和OutPutStream
两个类提供和数据操作相关的方法,后有其他不同类型的数据控制子类来继承这两个类
###字节流FileInputStream读取文件
首先在自己的项目中新建文件夹,命名为file
,右键该文件夹->Make Directory as
->Resources Root
,在file
中创建一个txt文件,随便写点东西
示例
public class IOTest {
public void inputFileTest() throws IOException {
FileInputStream fileInputStream = new FileInputStream(new File("file/IOTest1.txt"));
int byt = 0;
while ((byt = fileInputStream.read()) != -1) {
System.out.print((char)byt);
}
fileInputStream.close();
}
}
输出结果:
Lowell Here
先将数据一个字节一个字节读取放入byt
中,再输出byt
,由于FileInputStream
是字节流,我们需要将类型从int
转换为char
,进而输出为字符,而不是ASCII码。
注意:结尾一定需要把流关闭.close()
,否则内存将爆炸
###字节流FileOutPutStream写入文件 示例
public class IOTest {
public void outputFileTest() throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("file/IOtest2.txt");
byte[] bytes = "I'am testing.".getBytes(StandardCharsets.UTF_8);
for(int i = 0 ; i < bytes.length ; i++) {
fileOutputStream.write(bytes[i]);
}
fileOutputStream.close();
}
}
运行结果:
此时,
file
中出现了一个IOtest2.txt
,文件内容为I'am testing.
同样地,我们先将需要写入的数据放入bytes
中,再进行循环写入到IOtest2.txt
中。
###buff缓冲复制文件
FileInputStream
和FileOutputStream
不仅可以用于操作文本文件,亦可以操作媒体文件,例如图片
示例
public class IOTest {
public void copyFileTest() throws IOException {
FileInputStream fileInputStream = new FileInputStream("file/1.jpg");
FileOutputStream fileOutputStream = new FileOutputStream("file/2.jpg");
byte[] buff = new byte[1024];
int bytes;
while((bytes = fileInputStream.read(buff)) != -1) {
fileOutputStream.write(buff, 0, bytes);
}
fileInputStream.close();
fileOutputStream.close();
}
}
运行结果:
buff作为一个缓冲,先将1024个字节放入buff
中,再进行写入,这样相较于之前一个一个字节输出,效率明显提高了,就类似于搬货物,我们一箱一箱地搬,而不是一件一件地搬。
###buffered字节缓冲流
Buffer
类是 java.nio
的构造基础,一个Buffer
对象是固定数量的数据的容器,其作用是一个存储器或者分段运输区,数据可被存储并在之后用于检索
示例
public class IOTest {
public void bufferFileTest() throws IOException {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("file/IOtest1.txt"));
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("file/IOTest3.txt"));
int bytes;
while((bytes = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(bytes);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
运行结果:
file
中出现了一个IOtest3.txt
,文件内容和IOtest1.txt
一样
BufferedInputStream
和BufferedOutputStream
中接收的是对象
###字符流FileReader和FileWriter 这两种类非常适合用来读取文本文件,且使用时不在需要设置缓冲 示例
public class IOTest {
public void FileReaderTest() throws IOException {
FileReader fileReader = new FileReader("file/IOTest1.txt");
int ch;
while ((ch = fileReader.read()) != -1) {
System.out.print((char)ch);
}
fileReader.close();
}
public void FIleWriterTest() throws IOException {
FileWriter fileWriter = new FileWriter("file/4.txt");
fileWriter.write("I'am testing FileWriter!");
fileWriter.close();
}
}
运行结果:
Lowell Here
file
中出现一个4.txt
,文件内容为I'am testing FileWriter!
###BufferedReader 和 BufferedWriter 示例
public class IOTest {
public void bufferedWriterTest() throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("file/IOtest1.txt"));
bufferedWriter.write("123");
bufferedWriter.newLine();//换行
bufferedWriter.write("456");
bufferedWriter.close();
}
}
运行结果
file
中IOtest1.txt
内容更新为 123 456
public class IOTest {
public void bufferedReaderTest() throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("file/IOtest1.txt"));
String str;
while((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
bufferedReader.close();
}
}
输出结果:
123 456 该方法
.readLine()
可以在输出时整行输出
参考资料: Java Documentation IO Stream 菜鸟教程 Java 流(Stream)、文件(File)和IO [Apache Commons IO](