从文件中读取文本,并将其与字符序列进行比较



我可以得到建议,还是喜欢应该使用或已知的内容来完成此任务的建议(以我猜想最基本的方式)。如果有人愿意编写一个很棒的代码,但是关于必要知识或技术的模糊答案就足够了。

我想要一个程序,在开始时,您输入字符要么通过按Enter进行seperata,要么按字符串将其切成阵列的单独项目(我猜) - 由逗号隔开的字符 - 然后将包含一系列条目的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;
    }
}

相关内容

  • 没有找到相关文章

最新更新