【问题标题】:Read txt file and return an array of objects that has multiple fields读取 txt 文件并返回一个包含多个字段的对象数组
【发布时间】:2018-05-06 05:39:04
【问题描述】:

我有一个文本文件,其中每一行是一个Movie 实例,Movie 对象的字段由制表符分隔。 我需要阅读它并返回一个 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 而不是数组。将此行放在 while 之前: List movies = new ArrayList();并在里面同时向该列表中添加一个新电影,如下所示:movies.add(new Movie(...));
  • 您可以使用 ArrayList 代替数组。这将解决您的问题。

标签: java arrays object bufferedreader


【解决方案1】:

你已经几乎明白了。您可以从已提取的字段(如 titleratingsynopsisactors 等)创建 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 块。

【讨论】:

    【解决方案2】:

    做类似的事情

     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 al.toArray();
    

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 2019-02-05
      • 1970-01-01
      • 2023-01-24
      • 2018-12-29
      • 1970-01-01
      相关资源
      最近更新 更多