【问题标题】:Reading text from file and comparing it to seriers of characters从文件中读取文本并将其与一系列字符进行比较
【发布时间】:2019-03-20 15:09:18
【问题描述】:

我能否获得推荐或喜欢关于应该使用什么或知道什么来完成这项任务的建议(我猜是最基本的方式)。如果有人愿意编写一个很棒的代码,但对必要的知识或技术的模糊回答就足够了。

我想要一个程序,在开始时您输入的字符可以通过按 enter 分隔,也可以是一个字符串,该字符串可能像被分割成一个数组的单独项目(我猜)——用逗号分隔的字符——那将是然后与一个包含一系列条目的 txt 文件相比,只有那些包含一些(意味着更短)或所有在开始时提供的字符才会被打印,甚至打印也会被一个长度分隔初级词汇)。

关于如何做到这一点的任何想法?此外,是否可以将结果打印在命令行以外的其他地方,例如另一个 txt 文件?需要在java中执行此操作。谢谢。

【问题讨论】:

  • 是的,用 Java 编程。 SO 不是一个建议论坛。
  • 好吧,对于这类问题,这个论坛的错误部分或错误的站点是一起吗?如果是这样的话,你知道任何类似的好信息来源(或建议论坛)吗,例如,我不知道如何阅读 txt 文件,好吧,我会试着弄清楚其余的,即使你会是都很笨重。

标签: java string file text compare


【解决方案1】:

看看下面的例子:

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

}

【讨论】:

  • 谢谢。我会调查这个,到目前为止我不知道这里的一半。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
相关资源
最近更新 更多