Java 如何将内容从文本文件存储到 arraylist



首先,我有一个电影类,

public class Movie {
private int duration;
private String title;
private int year;
private ArrayList <Movie> movies;
public Movie() {
}
public Movie(int duration,String title,int year) {  
this.duration = duration;
this.title = title;
this.year = year;
}

public int getDuration() {
return duration;
}

public void setDuration(int duration) {
this.duration = duration;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String toString() {
return "Duration:" + this.duration + ",, "
+ "Title:" + this.title + ",, " + "Year:"
+ this.year;
}
}

我有一个管理课来做一些操作。我在文本文件中有一些电影,我想将它们存储到Movie类型的ArrayList中。因此,为此,我创建了一个方法,如下所示。

public void textFileToArrayList() {
movie = new ArrayList<Movie>();
BufferedReader br = null;
Movie e = null;
try {
br = new BufferedReader(new FileReader("movies.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line;
try {
while ((line = br.readLine()) != null) {
String data[] = line.split("t");
e = new Movie();
e.setDuration(Integer.parseInt(data[0]));
e.setTitle(data[1]);
e.setYear(Integer.parseInt(data[2]));
movie.add(e);
}
for(Movie i : movie){
System.out.println(i.getDuration()+","+i.getTitle()+","+i.getYear());
}
br.close();
} catch (IOException io) {
io.printStackTrace();
}
}

我的问题是,当我调试时,我看到解析很好,但由于某种原因,我的电影ArrayList总是空的。我在使用ArrayList时无法访问我的文本文件。我可以解析它,但我无法存储和打印。

我的完整代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.List;
import sait.mms.problemdomain.Movie;

public class MovieManagementSyste {
ArrayList<Movie> movi = new ArrayList<Movie>();
public MovieManagementSyste() {
textFileToArrayList();

displayMenu();

}
public void textFileToArrayList() {
movi = new ArrayList<Movie>();
BufferedReader br = null;
Movie e = null;
try {
br = new BufferedReader(new FileReader("movies.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line;
try {
while ((line = br.readLine()) != null) {
String data[] = line.split(",");
e = new Movie();
e.setDuration(Integer.parseInt(data[0]));
e.setTitle(data[1]);
e.setYear(Integer.parseInt(data[2]));
movi.add(e);
}
for(Movie i : movi){
System.out.println(i.getDuration()+","+i.getTitle()+","+i.getYear());
}
br.close();
} catch (IOException io) {
io.printStackTrace();
}
}

我只是运行代码,使用:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Movies {
private List<Movie> movi;
public static void main(String[] args) {
Movies m = new Movies();
m.textFileToArrayList();
}
public void textFileToArrayList() {
movi = new ArrayList<Movie>();
BufferedReader br = null;
Movie e = null;
try {
br = new BufferedReader(new FileReader("movies.txt"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String line;
try {
while ((line = br.readLine()) != null) {
String data[] = line.split(",");
e = new Movie();
e.setDuration(Integer.parseInt(data[0]));
e.setTitle(data[1]);
e.setYear(Integer.parseInt(data[2]));
movi.add(e);
}
for(Movie i : movi){
System.out.println(i.getDuration()+","+i.getTitle()+","+i.getYear());
}
br.close();
} catch (IOException io) {
io.printStackTrace();
}}
private static class Movie{
private int duration;
private String title;
private int year;
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
}
}

。对我来说效果很好。电影列表将打印到控制台。

最新更新