【问题标题】:How do I print lines from a command-line input file to a command-line output file?如何将命令行输入文件中的行打印到命令行输出文件?
【发布时间】:2014-05-15 16:00:29
【问题描述】:

输入文件有这样的文本行:

allison wesley 28011990

peter smith 05071992

我的代码(尝试将其打印到输出文件)如下(我将输入和输出都作为命令行参数提供):

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class FormatNames{
    public static void main(String[] args) throws FileNotFoundException{
        Scanner in = new Scanner(args[0]);
        PrintWriter out = new PrintWriter(args[1]);
        while(in.hasNext()){
            String str = in.next();
            out.print(""+str);
        }
        in.close();
        out.close();
    }
}

有谁知道为什么这不起作用? 目前,它输出到文件的唯一内容是输入文件的名称

谢谢

【问题讨论】:

标签: java input output command-line-arguments bluej


【解决方案1】:

您正在使用将字符串作为输入的 Scanner 构造函数:http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#Scanner(java.lang.String)

尝试使用接受类似 File 的构造函数:

new Scanner(new File(arg[0]));

【讨论】:

  • 非常感谢 :)
猜你喜欢
  • 1970-01-01
  • 2012-02-18
  • 2013-10-08
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 2015-06-23
  • 2015-07-28
  • 1970-01-01
相关资源
最近更新 更多