美文网首页
进程数据获取,动态编译,RandomAccessFile

进程数据获取,动态编译,RandomAccessFile

作者: 云承寒 | 来源:发表于2017-06-02 00:05 被阅读0次
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;
    }

}

相关文章

  • 进程数据获取,动态编译,RandomAccessFile

    RunTime Runtime用于表示Jvm运行时的状态,封装了Jvm进程,单例模式。 RunTime提供exec...

  • java火焰图工具

    1 下载编译async-profiler 2 获取java进程的pid 3 采集数据生成火焰图 4 打开浏览器查看...

  • JAVA的RandomAccessFile

    一、RandomAccessFile简介 RandomAccessFile既可以读取文件内容,也可以向文件输出数据...

  • 更多shell命令

     管理进程 获取磁盘统计信息 挂载新磁盘 排序数据 归档数据 实时显示进程 结束进程 kill+PID ...

  • note_4.1_Linux目录结构与根文件系统

    Linux文件系统 Linux:glibc程序编译方式: 动态链接 静态编译 进程的类型:终端:硬件设备,关联一个...

  • RandomAccessFile 与 内存映射文件

    RandomAccessFile是用来访问那些保存数据记录的文件的RandomAccessFile是用来访问那些保...

  • 动态获取数据

    从一个文件或服务器获取数据 要点有哪些?网络上的数据通常使用 JSON 格式化。JSON 是基于文本且易读的。da...

  • 马哥linux培训第三天笔记

    文件系统 程序编译方式 动态编译 依赖系统共享库文件,节约内存资源 静态编译 体积大,依赖的库包含在程序内部 进程...

  • VUE中如何动态编译js

    需求:动态获取一段字符串类型的js脚本,动态编译它并且可以完美在vue中运行与之交互。实现:动态编译js的方式有e...

  • python 线程

    进程=程序+数据集+进程控制块 进程就是一个程序在一个数据集上的一次动态执行过程。 进程一般由程序、数据集、进程控...

网友评论

      本文标题:进程数据获取,动态编译,RandomAccessFile

      本文链接:https://www.haomeiwen.com/subject/qvjyfxtx.html