2

现在我已经设置了这个程序,我可以在命令行中输入船只的名称,即 C:\java Proj3 "Boat 1" "Boat 2" "Boat 3" 并将结果打印到命令行基于眼镜。相反,我想在命令行 C:\java Proj3 "C:\class\Java\boatnames.txt" "C:\class\Java\results.txt" 中输入类似这样的内容,因此参数来自指定的文件结果打印在文本文件中,而不是在屏幕上。我将 println 更改为 printf,但到目前为止就是这样。我删除了所有其他失败的尝试。我什至尝试创建另一个名为 createfile.java 的类,它具有私有 Formatter x;和一些 openFile、closeFile 和 addRecords 方法,但是如果我将输出移到那里并尝试将其放入 addRecords 中,它不会

这是我的代码(我没有包含其他类,因为我需要做的就是将命令行中的 args 替换为命令行中 txt 文件中的 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();
        }




    }
}
4

4 回答 4

3

Right now, the top-level program logic is contained in one monolithic main() method. To make life easier, you should break it down into logical pieces and implement each piece in a separate method. For instance, your main() method might look something like this:

public static void main(String[] args) {
    ArrayList<Boat> boats = getBoats(args[0]);
    PrintStream out = getOutputStream(args[1]);
    printBoats(boats, out);
    out.close();
}

Then you need to write the support routines:

private ArrayList<Boat> getBoats(String inputFileName) {
    ...
}
PrintStream getOutputStream(String outputFileName) {
    ...
}
void printBoats(ArrayList<Boat> boats, PrintStream output) {
    ...
}

You will probably have to make it all a little more complicated to deal with I/O exceptions (missing files, write permissions, etc.). You'll also have to modify the methods Boat.printBoat() and Boat.whatIsBoatState() to take a PrintStream argument.

于 2012-09-25T01:52:46.307 回答
3

最简单的方法是使用现有的库来读取文件。一个非常常用的库是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
    ...
}
于 2012-09-25T01:47:43.640 回答
1

I've not tested this, but I've not tested it any great detail

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();
            }

        }

    }

}

The other problem is, you're going to need to provide some additional functionality to your Boat class

Namely, I used boat.toString() which needs to return the value Boat.printBoat() was printing and boat.getBoatState() which needs to return the value that Boat.getBoatState() was printing

于 2012-09-25T01:52:57.847 回答
1

按照这个

java-将参数传递给main方法

获取文件基不绝对路径并获取文件对象然后读取文件对象以获取内容。

于 2012-09-25T01:42:56.960 回答