English 中文(简体)
乌班图:从Java的txt档案中转引
原标题:Ubuntu: Redirection from a .txt file in Java

这应该很简单,但出于某种原因,我无法从 file子档案和产出中提出论点。 我的守则如下:

public class Lab2 {

static Scanner input = new Scanner(System.in);

public static void main(String[] args)
{   
    Lab2_CLA.Lab2_CLA(args);        
}
}

public class Lab2_CLA {

public static void Lab2_CLA(String[] data)
{
    System.out.printf("%s, %s: %s%n", data[1], data[0], data[2]);
}
}

我汇编了该守则,如果我通过终点站提出其论点的话,该守则是行之有效的。 注

java lab2/Lab2 John Doe 12345678

印刷:Doe, John: 12345678

However, how can I get it to read data from a .txt file by typing the following into the terminal: "java lab2/Lab2 < input.txt"? Whenever I do this, I get the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at lab2.Lab2_CLA.Lab2_CLA(Lab2_CLA.java:12)
at lab2.Lab2.main(Lab2.java:34)
最佳回答

投递权将档案寄给方案第6部分(标准投入文件),而不是其指挥参数。 在不相干的情况下,你可以通过使用背书操作者取得你想要的效果:

java lab2/Lab2 `cat input.txt`

简言之,执行背书与执行结果之间的案文将插入其位置。

EDIT (对应OP s edit):

你们摆脱了约束性的例外,因为除了重新定位外,方案没有达到任何指挥线参数,然而,压力阵列的零1和2却难以解决。

Should for some reason the backtick based solution not be possible for you the only option will be to modify your program to either read its stdin (technique show here Java file input as command line argument), or accept a filename as an argument, and read that file.

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签