Java 如何在此代码中输入文件?
我还是一个Java新手,我有这个代码。我不知道如何将输入文件传递给代码。我正在使用EclipseJunoJava 如何在此代码中输入文件?,java,eclipse,file-io,Java,Eclipse,File Io,我还是一个Java新手,我有这个代码。我不知道如何将输入文件传递给代码。我正在使用EclipseJuno public static void main(String[] args) { In in = new In(args[0]); // input file int N = in.readInt(); // N-by-N percolation system // turn on animation mode StdDraw.
public static void main(String[] args) {
In in = new In(args[0]); // input file
int N = in.readInt(); // N-by-N percolation system
// turn on animation mode
StdDraw.show(0);
// repeatedly read in sites to open and draw resulting system
Percolation perc = new Percolation(N);
draw(perc, N);
StdDraw.show(DELAY);
while (!in.isEmpty()) {
int i = in.readInt();
int j = in.readInt();
perc.open(i, j);
draw(perc, N);
StdDraw.show(DELAY);
}
}
无论何时运行,都会出现以下异常:
线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:0位于PercolationVisualizer.main(PercolationVisualizer.java:42)
什么可能导致此异常?您能耐心地告诉我如何在代码中调用输入文件吗?有关向程序中添加参数的信息,请参阅。或者,您可以直接在代码中指定文件名,而不是从args读取文件名
基本上,指南指示用户进入运行菜单,然后“运行…”(在最近的Eclipse版本中实际上是“运行配置…”),为所需项目选择适当的运行配置,单击参数选项卡,并在“程序参数”部分输入参数(如文件名),用空格分隔。对于使用IntelliJ的用户,可以通过运行->编辑配置设置程序参数。向下看窗口中间,找到“程序参数”字段。然后将路径添加到测试文件并保存。哪一个是第42行?如果它是第一个what(我猜是),那么您会得到异常,因为您没有向应用程序传递任何命令行参数。使用Eclipse运行程序时,您可以在启动配置的
arguments
选项卡中指定命令行参数。见和。