现在我将此程序设置为我可以在命令行中输入船名,即C:\ java Proj3“Boat 1”“Boat 2”“Boat 3” 并根据规范将结果打印到命令行。 相反,我想在命令行C:\ java Proj3“C:\ class \ Java \ boatnames.txt”“C:\ class \ Java \ results.txt”中输入类似的内容。 所以args来自指定的文件,结果打印在文本文件而不是屏幕上。我将println改为printf,但到目前为止就是这样。我删除了所有其他失败的尝试。我甚至尝试创建另一个名为createfile.java的类,该类具有私有的Formatter x;和一些中openFile,closeFile和addRecords方法,但如果我移动输出那边,并尝试把它放在addRecords,它不知道变量i是什么,我猜必须有一个更简单的方法,我不必创建该createfile类。
这是我的代码(我没有包含其他类,因为我需要做的就是用命令行中的txt文件中的args替换命令行中的args):
import java.util.*;
import java.io.*;
import java.lang.*;
import java.swing.*;
public class Proj3 {
public static void main(String args[]) {
Boat[] Boats;
char firstChar;
char secondChar;
int i;
Boats = new Boat[args.length];
for (i = 0; i < args.length; i++) {
firstChar = args[i].toUpperCase().charAt(0);
secondChar = args[i].toUpperCase().charAt(1);
if ((firstChar == 'B') || (firstChar == 'C') || (firstChar == 'N')) {
Boats[i] = new SailBoat();
} else {
Boats[i] = new RaceBoat();
}
Boats[i].setName(args[i]);
if ((secondChar == 'A') || (secondChar == 'E')) {
Boats[i].goFast();
} else {
Boats[i].goSlow();
}
}
for (i = 0; i < args.length; i++) {
System.out.printf("Boat number " + (i + 1) + " - \n");
System.out.printf(" ");
Boats[i].printBoat();
System.out.printf(" ");
Boats[i].whatIsBoatState();
}
}
}
答案 0 :(得分:2)
最简单的方法是使用现有的库来读取文件。一个非常常用的库是apache commons-io,它具有FileUtils.readLines()
实用程序方法。
import org.apache.commons.io.FileUtils;
// pass the filename is to your program on the command line
List<Sting> lines = FileUtils.readLines(new File(args[0]));
for (String line : lines) {
// use "line" instead of args[i] in your current code
...
}
答案 1 :(得分:2)
现在,顶级程序逻辑包含在一个单片main()
方法中。为了使生活更轻松,您应该将其分解为逻辑部分并以单独的方法实现每个部分。例如,您的main()
方法可能如下所示:
public static void main(String[] args) {
ArrayList<Boat> boats = getBoats(args[0]);
PrintStream out = getOutputStream(args[1]);
printBoats(boats, out);
out.close();
}
然后你需要编写支持例程:
private ArrayList<Boat> getBoats(String inputFileName) {
...
}
PrintStream getOutputStream(String outputFileName) {
...
}
void printBoats(ArrayList<Boat> boats, PrintStream output) {
...
}
您可能必须使处理I / O异常(丢失文件,写入权限等)变得更加复杂。您还必须修改方法Boat.printBoat()
和Boat.whatIsBoatState()
以获取PrintStream
参数。
答案 2 :(得分:1)
我没有对此进行测试,但我没有对它进行过任何细节测试
public static void main(String[] args) {
if (args.length == 2) {
String inFileName = args[0];
String outFileName = args[1];
File inFile = new File(inFileName);
if (inFile.exists()) {
try {
List<Boat> boats = new ArrayList<Boat>(25);
// Read the "boats" file...
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(inFile));
String text = null;
while ((text = br.readLine()) != null) {
char firstChar = text.toUpperCase().charAt(0);
char secondChar = text.toUpperCase().charAt(1);
Boat boat = null;
if ((firstChar == 'B') || (firstChar == 'C') || (firstChar == 'N')) {
boat = new SailBoat();
} else {
boat = new RaceBoat();
}
boat.setName(text);
if ((secondChar == 'A') || (secondChar == 'E')) {
boat.goFast();
} else {
boat.goSlow();
}
boats.add(boat);
}
} finally {
try {
br.close();
} catch (Exception e) {
}
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(outFileName, false));
for (int index = 0; index < boats.size(); index++) {
bw.write("Boat number " + (index + 1) + " - ");
bw.newLine();
bw.write(" " + boat.toString() + " " + boat.getBoatState());
}
bw.flush();
} finally {
try {
bw.close();
} catch (Exception e) {
}
}
} catch (IOException exp) {
exp.printStackTrace();
}
}
}
}
另一个问题是,您需要为Boat
课程提供一些额外的功能
即,我使用boat.toString()
需要返回正在打印的值Boat.printBoat()
,boat.getBoatState()
需要返回Boat.getBoatState()
正在打印的值
答案 3 :(得分:0)