首页 > 技术文章 > eclipse重定向输入输出到文件

lyf22 2016-04-18 17:44 原文

最近在学习算法第四版,为了要用作者给的测试数据alg4-data,需要将数据直接导入到程序中。在作者的示例代码里用了重定向来做这个事情,但是在eclipse里使用重定向很不方便,查了很多资料,都说是在右键Run As 中的Run Configurations 里的Arguments里添加program Arguments参数。试过很多次无效,放弃。然后发现在common选项里有一个Standard input and output,把Input File选好路径点击run就行了。但是run了之后要在控制台中按ctrl + Z程序才能运行完成。

我的eclipse版本为

Version: Mars.1 Release (4.5.1)
Build id: 20150924-1200

如果还是不行,

方法一:直接用Scanner类。

直接读输入:Scannerstdin = new Scanner(System.in); 用crtl+z来终止输入

从文本读入:Scannerstdin = new Scanner(new File("文件名"));

            while(stdin.hasNext()){

int key =stdin.nextInt();

  }

方法二:在eclispse编译好后,在命令行里执行吧。
参考:http://www.cnblogs.com/zhcncn/p/4689225.html

http://leetschau.github.io/blog/2013/01/23/125458/

推荐阅读