我想反击文件的行,在第二遍我想采取每一行并操纵它.它没有编译错误,但它不能进入第二个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
这里有两件事:
InputStream
s和Reader
s是一次性结构:一旦你将它们读到最后,你需要明确地回放它们(如果它们支持倒带),或者你需要关闭它们(总是关闭你的流和读者!)并打开一个新的.
但是在这种情况下,两次传递是完全没有必要的,只需使用动态增长的结构来收集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()
因为在这种情况下,它更短,更合适.此外,它不会消耗一条线.
这里有两件事:
InputStream
s和Reader
s是一次性结构:一旦你将它们读到最后,你需要明确地回放它们(如果它们支持倒带),或者你需要关闭它们(总是关闭你的流和读者!)并打开一个新的.
但是在这种情况下,两次传递是完全没有必要的,只需使用动态增长的结构来收集Movie
对象而不是数组:ArrayList
例如.
最简单的方法是重新重置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()
因为在这种情况下,它更短,更合适.此外,它不会消耗一条线.