读取 txt 文件和 return 具有多个字段的对象数组
Read txt file and return an array of objects that has multiple fields
我有一个文本文件,其中每一行都是一个 Movie
实例,并且 Movie
对象的字段由制表符分隔。
我需要阅读它和 return 一个 array
的对象(每一行),它有多个字段。我不知道如何制作 Movie
对象(即 Movie[]
)和 return
的数组。
我正在阅读的示例文本文件:
id title price
001 titanic 2
002 lady bird 3
以下是我目前得到的。
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;
}
}
做类似
的事情
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);
}
}
和 return 最后是这样的:
return al.toArray();
您几乎明白了。您可以从提取的字段(如 title
、rating
、synopsis
、actors
等)创建 Movie
对象,并将它们添加到您的array
.
此外,我建议您对电影使用 ArrayList
而不是 array
(除非您绝对确定自己拥有的电影数量)
您的 loadMovies
方法如下所示:
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;
}
请注意,我在您的原始代码中也合并了 variable
声明和 try-catch
块。
我有一个文本文件,其中每一行都是一个 Movie
实例,并且 Movie
对象的字段由制表符分隔。
我需要阅读它和 return 一个 array
的对象(每一行),它有多个字段。我不知道如何制作 Movie
对象(即 Movie[]
)和 return
的数组。
我正在阅读的示例文本文件:
id title price
001 titanic 2
002 lady bird 3
以下是我目前得到的。
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;
}
}
做类似
的事情 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);
}
}
和 return 最后是这样的:
return al.toArray();
您几乎明白了。您可以从提取的字段(如 title
、rating
、synopsis
、actors
等)创建 Movie
对象,并将它们添加到您的array
.
此外,我建议您对电影使用 ArrayList
而不是 array
(除非您绝对确定自己拥有的电影数量)
您的 loadMovies
方法如下所示:
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;
}
请注意,我在您的原始代码中也合并了 variable
声明和 try-catch
块。