当前位置:  开发笔记 > 编程语言 > 正文

如何在Java中读取相同的文件两次?

如何解决《如何在Java中读取相同的文件两次?》经验,为你挑选了2个好方法。

我想反击文件的行,在第二遍我想采取每一行并操纵它.它没有编译错误,但它不能进入​​第二个while ((line = br.readLine()) != null).是否有不同的方法来获取文件的行(电影)并存储在数组中?

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

biziclop.. 6

这里有两件事:

    InputStreams和Readers是一次性结构:一旦你将它们读到最后,你需要明确地回放它们(如果它们支持倒带),或者你需要关闭它们(总是关闭你的流和读者!)并打开一个新的.

    但是在这种情况下,两次传递是完全没有必要的,只需使用动态增长的结构来收集Movie对象而不是数组:ArrayList例如.


小智.. 5

最简单的方法是重新重置br.

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()因为在这种情况下,它更短,更合适.此外,它不会消耗一条线.



1> biziclop..:

这里有两件事:

    InputStreams和Readers是一次性结构:一旦你将它们读到最后,你需要明确地回放它们(如果它们支持倒带),或者你需要关闭它们(总是关闭你的流和读者!)并打开一个新的.

    但是在这种情况下,两次传递是完全没有必要的,只需使用动态增长的结构来收集Movie对象而不是数组:ArrayList例如.



2> 小智..:

最简单的方法是重新重置br.

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()因为在这种情况下,它更短,更合适.此外,它不会消耗一条线.

推荐阅读
yzh148448
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有