如何修改 main 方法以从命令行获取输入和输出?
how can i modify main method to get input and output from the comandline?
我不想硬编码来读取 infile
和 outfile
,而是想从命令行读取。我该怎么做?
/**
* reads a file and creates a histogram from it
* @param args string of argument
* @precondition none
*/
public static void main(String[] args) {
Histogram hist = new Histogram();
FileInputController input = new FileInputController(hist);
FileOutputController output = new FileOutputController(hist);
try {
input.readWords("infile.txt");
output.writeWords("outfile.txt");
} catch (FileNotFoundException exception) {
System.out.println("exception caught! somthing went wrong: " + exception.getMessage());
} catch (IOException exception) {
System.out.println("exception caught! somthing went wrong: " + exception.getMessage());
} finally {
System.exit(1);
}
}
public static void main(String[] args) {
WordHistogram hist = new WordHistogram();
FileInputController input = new FileInputController(hist);
FileOutputController output = new FileOutputController(hist);
try {
input.readWords(args[0]); //args[0] is the first argument passed while launching Java
output.writeWords(args[1]); //args[1] is the second argument passed while launching Java
} catch (FileNotFoundException exception) {
System.out.println("exception caught! somthing went wrong: " + exception.getMessage());
} catch (IOException exception) {
System.out.println("exception caught! somthing went wrong: " + exception.getMessage());
} finally {
System.exit(1);
}
}
运行 你的程序像:java YourClassName infile.txt outfile.txt
在 main(String[] args)
中,args
是一个字符串数组,其中包含启动时传递的参数值 Java。
请务必阅读 Oracle docs about command line arguments 以进一步阅读。
像这样使用得到整数值
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
获取字符串使用这个
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
并且仅用于输出 System.out.println("your text here");
我不想硬编码来读取 infile
和 outfile
,而是想从命令行读取。我该怎么做?
/**
* reads a file and creates a histogram from it
* @param args string of argument
* @precondition none
*/
public static void main(String[] args) {
Histogram hist = new Histogram();
FileInputController input = new FileInputController(hist);
FileOutputController output = new FileOutputController(hist);
try {
input.readWords("infile.txt");
output.writeWords("outfile.txt");
} catch (FileNotFoundException exception) {
System.out.println("exception caught! somthing went wrong: " + exception.getMessage());
} catch (IOException exception) {
System.out.println("exception caught! somthing went wrong: " + exception.getMessage());
} finally {
System.exit(1);
}
}
public static void main(String[] args) {
WordHistogram hist = new WordHistogram();
FileInputController input = new FileInputController(hist);
FileOutputController output = new FileOutputController(hist);
try {
input.readWords(args[0]); //args[0] is the first argument passed while launching Java
output.writeWords(args[1]); //args[1] is the second argument passed while launching Java
} catch (FileNotFoundException exception) {
System.out.println("exception caught! somthing went wrong: " + exception.getMessage());
} catch (IOException exception) {
System.out.println("exception caught! somthing went wrong: " + exception.getMessage());
} finally {
System.exit(1);
}
}
运行 你的程序像:java YourClassName infile.txt outfile.txt
在 main(String[] args)
中,args
是一个字符串数组,其中包含启动时传递的参数值 Java。
请务必阅读 Oracle docs about command line arguments 以进一步阅读。
像这样使用得到整数值
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
获取字符串使用这个
Scanner scanner = new Scanner(System.in);
String username = scanner.nextLine();
并且仅用于输出 System.out.println("your text here");