Java 读取txt文件并返回具有多个字段的对象数组
我有一个文本文件,其中每一行都是一个Java 读取txt文件并返回具有多个字段的对象数组,java,arrays,object,bufferedreader,Java,Arrays,Object,Bufferedreader,我有一个文本文件,其中每一行都是一个Movie实例,Movie对象的字段由一个选项卡分隔。 我需要读取它并返回对象(每行)的数组,该数组包含多个字段。我不知道如何制作Movie对象的数组(即Movie[])并返回它 我正在阅读的示例文本文件: id title price 001 titanic 2 002 lady bird 3 public static List<Movie> loadMovies() { // Initializ
Movie
实例,Movie
对象的字段由一个选项卡分隔。
我需要读取它并返回对象(每行)的数组,该数组包含多个字段。我不知道如何制作Movie
对象的数组(即Movie[]
)并返回它
我正在阅读的示例文本文件:
id title price
001 titanic 2
002 lady bird 3
public static List<Movie> loadMovies() {
// Initialize your movie list
List<Movie> movieList = new ArrayList<>();
String line = "", title, rating, synopsis, genre, director;
int id, price, runtime, index = 0;
String[] actors;
try (BufferedReader br = new BufferedReader(new InputStreamReader(movieIS))) {
while ((line = br.readLine()) != null) {
index++;
String[] data = line.split("\\t");
id = Integer.parseInt(data[0]);
title = data[1];
rating = data[2];
synopsis = data[3];
genre = data[4];
director = data[5];
actors = data[6].split(";");
price = Integer.parseInt(data[7]);
runtime = Integer.parseInt(data[8]);
// Create your Movie object here,
// note that I'm using constructor here,
// You can also use setters for optional fields as well
Movie movie = new Movie(id, title, rating, synopsis, genre, director, actors, price, runtime);
movieList.add(movie);
}
String[] lines = new String[index];
for (int i = 0; i < index; i++) {
lines[i] = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
//return movieList
return movieList;
}
以下是我目前掌握的信息。
public class Loader {
//private String csvFile;
private static final Resource tsvResource = new ClassPathXmlApplicationContext().getResource("classpath:movies.txt");
private static InputStream movieIS = null;
public Loader() {
try {
movieIS = tsvResource.getInputStream();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Movie[] loadMovies() {
BufferedReader br = null;
String line = "";
String[] tempArray = new String[100];
int id;
String title;
String rating;
String synopsis;
String genre;
String director;
String[] actors;
int price;
int runtime;
int index = 0;
try {
br = new BufferedReader(new InputStreamReader(movieIS));
while ((line = br.readLine()) != null) {
index++;
String[] data = line.split("\\t");
id = Integer.parseInt(data[0]);
title = data[1];
rating = data[2];
synopsis = data[3];
genre = data[4];
director = data[5];
actors = data[6].split(";");
price = Integer.parseInt(data[7]);
runtime = Integer.parseInt(data[8]);
}
String[] lines = new String[index];
for (int i = 0; i < index; i++) {
lines[i] = br.readLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
}
公共类加载器{
//私有字符串csvFile;
私有静态最终资源tsvResource=new ClassPathXmlApplicationContext().getResource(“classpath:movies.txt”);
私有静态InputStream movieIS=null;
公共加载程序(){
试一试{
movieIS=tsvResource.getInputStream();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共静态电影[]加载电影(){
BufferedReader br=null;
字符串行=”;
字符串[]临时数组=新字符串[100];
int-id;
字符串标题;
串级;
弦乐大纲;
弦乐体裁;
字符串控制器;
弦乐演员;
国际价格;
int运行时;
int指数=0;
试一试{
br=新的BufferedReader(新的InputStreamReader(电影));
而((line=br.readLine())!=null){
索引++;
String[]data=line.split(\\t“);
id=Integer.parseInt(数据[0]);
标题=数据[1];
评级=数据[2];
大纲=数据[3];
流派=数据[4];
董事=数据[5];
actors=数据[6]。拆分(“;”);
price=Integer.parseInt(数据[7]);
runtime=Integer.parseInt(数据[8]);
}
字符串[]行=新字符串[索引];
对于(int i=0;i
执行以下操作
ArrayList <> al = new ArrayList<Movie>();
int index = 0;
try{
br=new BufferedReader(new InputStreamReader(movieIS));
while((line=br.readLine())!=null){
index++;
String[] data=line.split("\\t");
id =Integer.parseInt(data[0]);
title=data[1];
rating=data[2];
synopsis=data[3];
genre=data[4];
director=data[5];
actors=data[6].split(";");
price= Integer.parseInt(data[7]);
runtime=Integer.parseInt(data[8]);
Movie mv = new Movie();
// load into mv
al.add(mv);
}
}
你已经差不多明白了。您可以从提取的字段(如标题
,评级
,概要
,演员
等)创建电影
对象,并将其添加到数组
此外,我建议您在电影中使用ArrayList
而不是array
(除非您完全确定您将拥有的电影数量)
您的loadMovies
方法如下所示:
id title price
001 titanic 2
002 lady bird 3
public static List<Movie> loadMovies() {
// Initialize your movie list
List<Movie> movieList = new ArrayList<>();
String line = "", title, rating, synopsis, genre, director;
int id, price, runtime, index = 0;
String[] actors;
try (BufferedReader br = new BufferedReader(new InputStreamReader(movieIS))) {
while ((line = br.readLine()) != null) {
index++;
String[] data = line.split("\\t");
id = Integer.parseInt(data[0]);
title = data[1];
rating = data[2];
synopsis = data[3];
genre = data[4];
director = data[5];
actors = data[6].split(";");
price = Integer.parseInt(data[7]);
runtime = Integer.parseInt(data[8]);
// Create your Movie object here,
// note that I'm using constructor here,
// You can also use setters for optional fields as well
Movie movie = new Movie(id, title, rating, synopsis, genre, director, actors, price, runtime);
movieList.add(movie);
}
String[] lines = new String[index];
for (int i = 0; i < index; i++) {
lines[i] = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
//return movieList
return movieList;
}
公共静态列表loadMovies(){
//初始化电影列表
List movieList=new ArrayList();
字符串行=”,标题,评级,大纲,流派,导演;
int id,price,runtime,index=0;
弦乐演员;
try(BufferedReader br=new BufferedReader(new InputStreamReader(movieIS))){
而((line=br.readLine())!=null){
索引++;
String[]data=line.split(\\t“);
id=Integer.parseInt(数据[0]);
标题=数据[1];
评级=数据[2];
大纲=数据[3];
流派=数据[4];
董事=数据[5];
actors=数据[6]。拆分(“;”);
price=Integer.parseInt(数据[7]);
runtime=Integer.parseInt(数据[8]);
//在此处创建电影对象,
//注意,我在这里使用构造函数,
//您还可以对可选字段使用setter
电影=新电影(id、标题、评级、概要、类型、导演、演员、价格、运行时间);
movieList.add(电影);
}
字符串[]行=新字符串[索引];
对于(int i=0;i
请注意,我已将变量
声明和try catch
块以及原始代码组合在一起。可以显示电影类吗?因为电影是从文件动态加载的,所以最好使用ArrayList而不是array。将此行放在while之前:List movies=new ArrayList();在里面的同时,向列表中添加一部新电影,如下所示:movies.add(newmovie(…);可以使用ArrayList而不是数组。这会解决你的问题。