使用BufferedReader从文本文件中读取段落

时间:2016-12-13 18:51:44

标签: java

我正在尝试给用户每日星座运势。我有一个看起来像这样的大文本文件:

  

白羊座

     

你身边永远不会有一个沉闷的时刻[继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [结束]。

     

牛牛

     

进入[继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [继续占星] [结束]。

此文本文件包含所有星座的所有星座图。我试图根据用户的输入打印出正确的段落。有人可以帮我这个吗?这就是我到目前为止所做的:

public static void getDailyHoroscope() throws IOException{
    Scanner sc = new Scanner(System.in);
    System.out.println("Please input your zodiac sign:");
    String sign = sc.nextLine();
    String file = ".txt file";

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;

        while ((line = br.readLine()) != null) {
            System.out.print(line);
        }
        br.close( );
    }
    catch(FileNotFoundException e) {
        System.out.println("Cannot find" + file);
}

我不知道该怎么办。这只会在控制台中吐出整个文本文件。我想根据用户的标志将段落分开输出。

4 个答案:

答案 0 :(得分:2)

好像你做了很多工作。

在你的情况下,它可能是一个商品想法,首先读取整个文件,按段落拆分,将其存储到某些集合,如Map<Sign, Paragraph>Set<Paragraph>,然后只搜索该集合中的条目需要。

答案 1 :(得分:0)

我的建议是继续阅读该文件,直到您点击用户的输入,然后是空行,然后继续阅读,直到您点击另一个新行后跟一个空行。

这样的事可能有用:

public static void getDailyHoroscope() throws IOException{
    Scanner sc = new Scanner(System.in);
    System.out.println("Please input your zodiac sign:");
    String sign = sc.nextLine();
    String file = ".txt file";

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        boolean print = false;
        while ((line = br.readLine()) != null) {
            //If our print flag is false and we found a line that's just our users's sign
            if((!print) && line.trim().equalsIgnoreCase(sign)){
                //set our print flag to true
                print = true;
            }
            if(print){
                //if we're printing, and the line we just read was completley empty, unset our print flag.
                if(line.trim().isEmpty()){
                    print = false;
                }
                System.out.println(line);
            }
        }
        br.close( );
    }catch(FileNotFoundException e) {
        System.out.println("Cannot find" + file);
    }
}

请记住,我在记事本中写了这个,可能不起作用。

考虑到上述帖子的一些注意事项: 在这个代码的当前状态中,如果它甚至可以工作,它可能只打印星座名称,然后点击一个空行并停止打印,你需要考虑到这种可能性。

答案 2 :(得分:0)

public static void getDailyHoroscope() throws IOException{
    Scanner sc = new Scanner(System.in);
    System.out.println("Please input your zodiac sign:");
    String sign = sc.nextLine();
    String file = "C:/Users/Laptop/Downloads/horoscope.txt";
    sc.close();
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        //include boolean exist sign, only because message
        boolean existSign = false;

        while ((line = br.readLine()) != null) {
            if (line.equalsIgnoreCase(sign)) {
                //skip first empty line
                br.readLine();
                line = br.readLine();
                while (line!=null && !(line.isEmpty())) {
                    System.out.println("\n"+line);
                    line = br.readLine();
                }

                existSign = true;
                break;
            }
        }
        if (!existSign)
            System.out.println("Please input zodiac sign which exists");

        br.close();
    } catch (FileNotFoundException e) {
        System.out.println("Cannot find" + file);
    }
}

在外部while循环读取行中,而行不等于符号。 进入如果读取一行跳过空行,用thar读取有用文本后的行。只要它到达结束或下一个空行,打印该行并读取下一行。在内部时,将existsSign设置为true并在外部打破。

答案 3 :(得分:-1)

尝试:

Scanner sc = new Scanner(System.in);
        System.out.println("Please input your zodiac sign:");
        String sign = sc.nextLine();
        String file = ".txt file";

        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            String line;

            String sign2 = "";
            String content = "";
            String nl = System.getProperty("line.separator");
            //I assummed that your text not end with [end] for each sign zodiac.
            while ((line = br.readLine()) != null) {
                String l = line.trim().toLowerCase();
                //Check if line is equal than some zodiac sign.
                String sign3 = "";
                if (l.equals("aries")) {
                    sign3 = "aries";
                } else if (l.equals("tauro")) {
                    sign3 = "tauro";
                } //complete here all zodiac sing with else if.

                //Check if sign is equal than sign enter by user and data content for this sign still not ready. 
                if (sign2.length() == 0 && sign3.length() != 0 && sign.toLowerCase().equals(sign3)) {
                    sign2 = sign3;
                } else {
                    //Check if we need concatenate data for this sign.
                    if (sign2.length() > 0 && sign3.length() == 0) {
                        content += line + nl;
                    } else {
                        //If this line is equal other sign and content data we finalized and break.
                        if (sign2.length() > 0 && sign3.length() != 0) {
                            break;
                        }
                    }
                }

            }
            br.close();

            System.out.println(content);
        } catch (Exception ex) {
            System.out.println("Cannot find" + file);
        }