簡體   English   中英

如何修改此Java程序以從命令行中指定的文件中讀取名稱?

[英]How can I modify this Java program to read in the names from a file designated in the command line?

現在我將這個程序設置到我可以在命令行中輸入船只名稱的地方,即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中,它就不知道我是什么變量了,我猜我必須有一個更簡單的方法不必創建該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();
        }




    }
}

最簡單的方法是使用現有的庫來讀取文件。一個非常常用的庫是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
    ...
}

現在,頂級程序邏輯包含在一個單片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參數。

按照這個

java-將參數傳遞給main方法

獲取文件base don絕對路徑並獲取文件對象然后讀取文件對象以拾取內容。

我沒有對此進行過測試,但我沒有對其進行任何細節測試

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()打印的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM