簡體   English   中英

從文件中讀取文本並將其與一系列字符進行比較

[英]Reading text from file and comparing it to seriers of characters

我可以得到關於應該使用什么或知道什么來完成這項任務的推薦或喜歡的建議(我猜是最基本的方式)。 如果有人願意編寫一個很棒的代碼,但對必要的知識或技術給出模糊的答案就足夠了。

我想要一個程序,在該程序中,您可以在開始時輸入通過按 Enter 分隔的字符或一個字符串,該字符串可以像切碎成數組的單獨項目一樣(我猜) - 字符由逗號分隔 - 然后將與一個包含一系列條目的 txt 文件,並且只有那些包含開頭提供的一些(意味着更短)或所有字符的文件才會被打印出來,甚至打印件可能會被條目的長度(單詞)。

關於如何做到這一點的任何想法? 此外,結果是否可以打印在命令行以外的其他地方,如另一個 txt 文件? 需要在java中執行此操作。 謝謝。

看看下面的例子:

public class SimpleExample {

    public static void main(String[] args) throws ClassNotFoundException {
        Scanner inputNumbers = new Scanner(System.in);
        List<Integer> listOfNumbersToStore = new ArrayList<>();
        List<Integer> listOfNumbersToCheck = new ArrayList<>();
        int number;
        String answer;
        boolean flag = true;
        // do code within a loop while flag is true
        do {
            // print message to screen
            System.out.print("Would you like to put new number to your file list (Y/N): ");
            // get answer (Y/N) to continue
            answer = inputNumbers.next();
            // if answer is Y or y
            if ("Y".equalsIgnoreCase(answer)) {
                // print message
                System.out.print("Put your number: ");
                // get input integer and assign it to number
                number = inputNumbers.nextInt();
                // add that number to a list of numbers to store to file
                listOfNumbersToStore.add(number);
            } else if ("N".equalsIgnoreCase(answer)) {
                flag = false;
            }

        } while (flag);
        writeToFile(listOfNumbersToStore);
        System.out.println("---------- Check numbers ----------");
        flag = true; // set it again to true
        //do code within a loop while flag is true
        do {
            System.out.print("Would you like to put new number to your check list (Y/N) : ");
            answer = inputNumbers.next();
            if ("Y".equalsIgnoreCase(answer)) {
                System.out.print("Put your number: ");
                number = inputNumbers.nextInt();
                listOfNumbersToCheck.add(number);
            } else if ("N".equalsIgnoreCase(answer)) {
                flag = false;
            }

        } while (flag);
        // get a list from a file
        List<Integer> readFromFile = readFromFile();
        // check if there are any common elements within compared lists
        boolean areThereAnyCommonElements = !Collections.disjoint(
                listOfNumbersToCheck, readFromFile);

        //create a new treeset used for containing unique elements and ordering it naturally, from 0 to n
        Set<Integer> set = new TreeSet<>(listOfNumbersToCheck);
        set.retainAll(readFromFile);
        // print these messages
        System.out.println("Are there any common integers between a list from a file and checking list? " + areThereAnyCommonElements);
        System.out.println("Those integers are: " + set.toString());
    }

    /**
     * List implements Seriazable interface, therefore store it to a file
     * serialized
     *
     * @param numberOfIntegers
     */
    public static void writeToFile(List<Integer> numberOfIntegers) {
        try {
            // create a file output stream to write to the file with the specified name. 
            FileOutputStream fileOutputStream = new FileOutputStream("tekstDataOutputStream");
            // writes the serialization stream header to the underlying file stream; 
            ObjectOutputStream dataOutputStream = new ObjectOutputStream(new BufferedOutputStream(fileOutputStream));
            // write a list to object output stream
            dataOutputStream.writeObject(numberOfIntegers);
            //close them
            dataOutputStream.close();
            fileOutputStream.close();
        } catch (IOException ioE) {
            System.err.println("JVM reported an error! Take a look: " + ioE);
        }
    }

    public static List<Integer> readFromFile() throws ClassNotFoundException {
        //create an empty list of integers
        List<Integer> result = new ArrayList<>();
        try {
            //opening a connection to an actual file
            FileInputStream fis = new FileInputStream("tekstDataOutputStream");
            //used for reading from a specified input stream
            ObjectInputStream reader = new ObjectInputStream(fis);
            //get that list 
            result = (List<Integer>) reader.readObject();
            //close streams
            reader.close();
            fis.close();

        } catch (IOException ioE) {
            System.err.println("JVM reported an error! Take a look: " + ioE);
        }
        return result;
    }

}

暫無
暫無

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

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