美文网首页
IDEA配置重定向功能

IDEA配置重定向功能

作者: mcaotuman | 来源:发表于2020-05-02 21:30 被阅读0次

    重定向

    《算法4》第一章1.1.10 的二分查找,在命令行中,书本给出的

    % java BinarySearch tinyW.txt < tinyT.txt

    相信很多人都有疑问,这是什么意思?

    其实“ < ” , " > " 这是重定向的提示符。

    这行命名的意思是:从tinyT.txt文件中读取一系列的数值,运行在BinarySearch这个类的main方法上,tinyW.txt作为args[]数值的第一个参数传递进去。

    我们首先看看这段程序:

    public class BinarySearch {
      
        public static int rank(int[] a, int key) {
            // 数组必须是有序的
            int lo = 0;
            int hi = a.length - 1;
            while (lo <= hi) {
                // 被查找的键要么不存在,要么必然存在于a[lo..hi]中
                int mid = lo + (hi - lo) / 2;
                if      (key < a[mid]) hi = mid - 1;
                else if (key > a[mid]) lo = mid + 1;
                else return mid;
            }
            return -1;
        }
      
        public static void main(String[] args) {
    
            // read the integers from a file
            In in = new In(args[0]);
            int[] whitelist = in.readAllInts();
    
            // sort the array
            Arrays.sort(whitelist);
    
            // read integer key from standard input; print if not in whitelist
            while (!StdIn.isEmpty()) {
                // 读取键值,如果不存在于白名单中则将其打印
                int key = StdIn.readInt();
                if (BinarySearch.rank(whitelist, key) == -1)
                    StdOut.println(key);
            }
        }
    }
    

    上述代码,用args[0] 读取tinyW.txt文件的内容,保存在了whitelist数组中,而 < tinyT.txt 采用了重定向标准输入,也就是说,系统直接读取了tinyT.txt作为了输入流,读取这个文件的内容保存在key变量。

    IDEA重定向输入的配置(MAC)

    1.保存你的测试数据文件在任意目录下,我这里把tinyT.txt和tinyW.txt保存在工程的testcase目录下。 1.png

    2.点击 Run -> Edit Configurations


    2.png

    3.Program arguments:指定tinyW.txt文件的路径

    4.Redirect input from:指定重定向输入文件tinyT.txt的路径

    3.png

    5.运行程序,ok!


    4.png

    ps:idea2018没有Redirect input from这个选项,我的idea的版本是2020.1

    IDEA重定向输出的配置(MAC)

    将标准输出重定向到一个文件

    % java RandomSeq 1000 100.0 200.0 < randomresult.txt

    以上命令行,在idea的配置如下:

    点击 Run -> Edit Configurations
    1.Program arguments:设置入参


    2.1.png

    2.save console output to file: 指定输出的文件路径


    2.2.png

    3.运行程序,ok!


    2.3.png

    如有需要在Eclipse配置重定向功能,请移步到https://www.jianshu.com/p/cbcee3e0e983

    相关文章

      网友评论

          本文标题:IDEA配置重定向功能

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