我是java的新手。我需要编写一个程序来读取包含以下数据的txt文件:
Franklin,Benjamin,74,bcbs of Oklahoma,18
Hamilton,Alexander,199,aetna,25
Thatcher,Margaret,65,aflack,3
Nixon,Richard,45,kaiser permanente,7
数据是名字,姓氏,年龄,公司,房间号码。程序应该采用这些字符串(?)并将它们格式化为一个打印到cmd提示符的表,该表应如下所示:
Last First Age Insurance RoomHamilton
下面列中的名称和数字。
我知道这应该与ArrayList有关,但我真的输了。
答案 0 :(得分:0)
这可以帮助您入门,是的,您应该使用列表。
try {
/** Reads all the lines of a file, and loops through each line */
for (final String line : Files.readAllLines(Paths.get("PATH_TO_FILE_HERE"))) {
/** To show you what the line looks like */
System.out.println(line);
/** Splits each line by commas */
String[] data = line.split(",");
/**
* Format the data here, then store it into a list
*/
}
} catch (IOException e) {
e.printStackTrace();
}
要打印到命令控制台......
/** On all operating systems... */
private String[] generateLaunchArgs(String exe) {
final String os = System.getProperty("os.name");
if (os.toLowerCase().contains("win")) {
return new String[] { "cmd", "/c", exe };
}
return new String[] { "bash", "-c", exe };
}
public void printToConsole(String message) {
Runtime.getRuntime().exec(generateLaunchArgs("echo " + message));
}