本文介绍了如何在Java中两次读取同一文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对文件的行数进行计数,在第二遍中,我想占用每一行并对其进行操作。它没有编译错误,但不能进入第二个 while((line = br.readLine())!= null)
是否有其他方法来获取文件的行(电影)并存储在数组中?

I want to counter the lines of the file and in the second pass i want to take every single line and manipulating it. It doesn't have a compilation error but it can't go inside the second while ((line = br.readLine()) != null) .Is there a different way to get the lines(movies) of the file and storing in an array ?

        BufferedReader br = null;

        try { // try to read the file
            br = new BufferedReader(new FileReader("movies.txt"));
            String line;
            int numberOfMovies = 0;
            while ((line = br.readLine()) != null) {
                numberOfMovies++;
            }
            Movie[] movies = new Movie[numberOfMovies]; // store in a Movie
                                                        // array every movie of
                                                        // the file
            String title = "";
            int id = 0;
            int likes = 0;
            int icounter = 0; // count to create new movie for each line
            while ((line = br.readLine()) != null) {
                line = line.trim();
                line = line.replaceAll("/t", "");
                line = line.toLowerCase();
                String[] tokens = line.split(" "); // store every token in a
                                                    // string array
                id = Integer.parseInt(tokens[0]);
                likes = Integer.parseInt(tokens[tokens.length]);
                for (int i = 1; i < tokens.length; i++) {
                    title = title + " " + tokens[i];
                }
                movies[icounter] = new Movie(id, title, likes);
                icounter++;
            }


        } catch (IOException e) {
            e.printStackTrace();
        }


推荐答案

最简单的方法是重置再次 br

Simplest way would be to reset br again.

try { // try to read the file
    br = new BufferedReader(new FileReader("movies.txt"));
    String line; int numberOfMovies = 0;
    while (br.hasNextLine()){
        numberOfMovies++;
    }
    br.close();
    Movie[] movies = new Movie[numberOfMovies];
    // store in a Movie
    // array every movie of
    // the file
    String title = "";
    int id = 0;
    int likes = 0;
    int icounter = 0;
    // count to create new movie for each line
    br = new BufferedReader(new FileReader("movies.txt"));
    while ((br.hasNextLine()) {
        line = line.trim();
        line = line.replaceAll("/t", "");
        line = line.toLowerCase();
        String[] tokens = line.split(" ");
        // store every token in a
        // string array
        id = Integer.parseInt(tokens[0]);
        likes = Integer.parseInt(tokens[tokens.length]);
        for (int i = 1; i < tokens.length; i++) {
            title = title + " " + tokens[i];
        }
        movies[icounter] = new Movie(id, title, likes);
        icounter++;
    }
} catch (IOException e) { e.printStackTrace(); }

我更改了 br.nextLine()!= null br.hasNextLine(),因为在这种情况下它更短且更合适。不会占用一行。

I changed br.nextLine() != null to br.hasNextLine() because it's shorter and more appropriate in this case. Plus it won't consume a line.

这篇关于如何在Java中两次读取同一文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:50