V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
SharkIng
V2EX  ›  Java

LInux系统中Java程序如何从一个文件中读取command line 形式作为input??

  •  
  •   SharkIng ·
    SharkIng · 2013-10-28 18:13:24 +08:00 · 3686 次点击
    这是一个创建于 3826 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现在这个系统需要一些input, 例如做什么(例如 login)回车后问你名字叫什么(例如Peter)回车后问你学生编号是什么(例如100000)

    然后现在我们有一个txt文件, 内容就是 login(回车)Peter(回车)100000 每个信息一行

    在Linux系统中, 运行Java文件在compile之后用java filename(回车)
    然后就可以输入上面说的信息了

    可是如果想要从这个txt里面作为input怎么做??

    原本想的是 java filename <inpput.txt 但是出现错误
    (java.lang.NullPointerException)
    是java本身写错了还是这个input file使用错了??

    求解
    4 条回复    1970-01-01 08:00:00 +08:00
    cloudzhou
        1
    cloudzhou  
       2013-10-28 18:29:07 +08:00
    http://stackoverflow.com/questions/1431551/command-line-pipe-input-in-java

    By executing "java Read < input.txt" you've told the operating system that for this process, the piped file is standard in. You can't then switch back to the command line from inside the application.

    If you want to do that, then pass input.txt as a file name parameter to the application, open/read/close the file yourself from inside the application, then read from standard input to get stuff from the command line.
    delo
        2
    delo  
       2013-10-28 19:19:48 +08:00 via iPhone
    cat input.txt | java filename 试试不保证有效
    msg7086
        3
    msg7086  
       2013-10-28 19:55:38 +08:00
    for循环,然后开一个长字符串,把操作指令和循环变量拼一起,最后塞给程序。
    ericson
        4
    ericson  
       2013-10-29 09:26:14 +08:00
    @SharkIng 信息不够, 至少给出:

    1. 读取输入的代码;
    2. java.lang.NullPointerException对应行的代码;
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   949 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 20:23 · PVG 04:23 · LAX 13:23 · JFK 16:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.