实现流水线时,需要将代码的输入输出改为从文件中读入写出,因为原代码中有大量输出函数,为了避免大幅度改动,便采用重定向。
头文件
import java.io.FileInputStream;
import java.util.Scanner;
import java.io.FileOutputStream;
import java.io.PrintStream;
代码段
instructionCache = new int[12];
try{
FileInputStream fis=new FileInputStream("C:\\Users\\input.txt");
System.setIn(fis);
String str = null;
Scanner sc=new Scanner(System.in);
while(sc.hasNextLine())
{
str = sc.nextLine();
String[] s = str.split("");
int num = s.length;
for(int i = 0;i < num;i++)
{
instructionCache[i] = Integer.parseInt(s[i]);
}
}
}
catch (Exception e) {
System.out.println("打开读取的文件出错");
e.printStackTrace();
}
try{
PrintStream ps=new PrintStream(new FileOutputStream("C:\\Users\\output.txt"));
System.setOut(ps);
}
catch (Exception e) {
System.out.println("打开输出的文件出错");
e.printStackTrace();
}
需要注意的是写上处理异常。