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

时间:2019-03-20 15:09:18

标签: java string file text compare

我可以得到建议或喜欢的建议以完成该任务的方式或方法(我认为是最基本的方式)。如果有人愿意写一个很棒的代码,但是对必要的知识或技术的模糊回答就足够了。

我想要一个程序,一开始您输入的字符要么是按回车键来分隔的,要么是一个字符串,该字符串可能会切成数组的各个项目(我想)(用逗号分隔的字符),然后将其与包含一系列条目的txt文件进行比较,然后仅打印包含开始时提供的某些(意思是较短的)字符或所有字符的txt文件,甚至连打印件都将以初级词汇)。

关于如何执行此操作的任何想法?此外,结果可以像其他txt文件一样在命令行以外的其他位置打印吗?需要在Java中执行此操作。谢谢。

1 个答案:

答案 0 :(得分:0)

看看下面的例子:

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

}