RunTime
-
Runtime用于表示Jvm运行时的状态,封装了Jvm进程,单例模式。
-
RunTime提供exec(),用于执行dos命令,返回值为Process对象,该对象表示操作系统的一个进程。
Process
-
Process 为抽象类。
-
获取Process两种方式
ProcessBuilder.start();
Runtime.exec();
Process提供的API
-
void destroy() 关闭进程
-
InputStream getErrorStream() 获取子进程的错误流
-
InputStream getInputStream()
-
OutputStream getOutputStream()
动态编译
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Run {
public static void main(String[] args) throws IOException {
String msg = "System.out.println(\"测试数据 \")";
test(msg);
}
public static void test(String msg) throws IOException {
//拼接字符串
StringBuilder builder = new StringBuilder();
builder.append("public class Test {");
builder.append("public static void main(String[] args) {");
builder.append(msg);
builder.append(";");
builder.append("}");
builder.append("}");
// System.out.println(builder);
//保存,注意保存的文件名要和类名一致,别问为什么,问了就挨打
FileOutputStream outputStream = new FileOutputStream("Test.java");
outputStream.write(builder.toString().getBytes());
outputStream.close();
//调用Java进程来编译Test.java
Process javacProcess = Runtime.getRuntime().exec("javac Test.java");
//如果出错打印进程中错误信息
InputStream errorInputStream = javacProcess.getErrorStream();
byte[] errorBuf = new byte[1024];
int len = 0;
while ((len = errorInputStream.read(errorBuf)) != -1) {
System.out.println(new String(errorBuf, 0, len));
}
errorInputStream.close();
//如果没错则调用java编译
Process javaProcess = Runtime.getRuntime().exec("java Test");
InputStream javaInputStream = javaProcess.getInputStream();
byte[] successBuf = new byte[1024];
int successLen = 0;
while ((successLen = javaInputStream.read(successBuf)) != -1) {
System.out.println(new String(successBuf, 0, successLen));
}
javaInputStream.close();
//删除生成的Java和.claa文件
new File("Test.java").delete();
new File("Test.class").delete();
}
}
RandomAccessFile
RandomAccessFile支持在文件的任何位置写出和读取数据,多用于多线程断点下载。
RandomAccessFile一个类包含了readXxx和writeXxx方法。
//写
public static void writeDemo(File file) throws IOException {
RandomAccessFile accessFile = new RandomAccessFile(file, "rw");
//设置文件大小,最多可设置1G
accessFile.setLength(1024 * 104);
accessFile.writeInt(10);
accessFile.writeDouble(10.5);
accessFile.writeUTF("测试"); //使用的是修改后的UTF,多两个字节
accessFile.close();
}
public static void readDemo(File file) throws IOException {
/**
* 第二个参数是模式
* 四种模式
* r 只读
* rw 读写,如果文件不存在会创建
*
* rws 读写写完后就同步底层设备
* rwd
*/
RandomAccessFile accessFile = new RandomAccessFile(file, "r");
//设置文件指针
accessFile.seek(1);
//设置文件指针跳过12个字节
accessFile.skipBytes(12);
int readInt = accessFile.readInt();
//获取文件指针位置
long pos = accessFile.getFilePointer();
System.out.println("文件指针位置:" + pos);
System.out.println(readInt + "");
accessFile.close();
}
断言
断言就是做预测,预测期望的结果和实际结果是否一致。
断言有期望值,真实值,当真实值和期望值相同则断言成功,否则失败。
断言的作用是用来做测试用例。
实现步骤
1.定义接口
/**
*数学接口
*/
public interface IMath {
/**
* 加法
*/
int getSum(int a, int b);
/**
* 除法
*/
int div(int a, int b);
}
2.生成测试用例
/**
*测试用例
*/
public class MathTest {
private IMath math = new MathImpl();
/**
* 写测试用例遵守的规范
* 测试方法必须是public修饰
* 没有返回值
* 不能传参数
* 原因:看我的注解模拟Juntil的实现原理
*/
@Test
public void testGetSum() {
int sum = math.getSum(2, 4);
//参数:断言失败的打印,期望值,真实值
Assert.assertEquals("失败", 6, sum);
}
//如果出现该算数异常表示断言成功
@Test(expected = ArithmeticException.class)
public void testDiv() {
int sum = math.div(4, 0);
Assert.assertEquals("失败", 0, sum);
}
@Test(timeout = 1000)
public void getTime() throws InterruptedException {
Thread.sleep(1200);
}
}
3.编写实现类
public class MathImpl implements IMath {
@Override
public int getSum(int a, int b) {
return a + b;
}
@Override
public int div(int a, int b) {
return a / b;
}
}
网友评论