简体   繁体   中英

Java How to store content from text file to arraylist

Firstly I have a Movie class like,

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;
     }
}

And I have a management class for doing some operations. I have some movies in a text file and I want to store them into the ArrayList of the Movie type. So, for doing this I create a method as you can see below.

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();
    }
}

My problem is, when I debugging, I see that parsing is fine but for some reason my movie ArrayList is always empty. I cannot reach my text file while using ArrayList . I can parse it but I cannot store and print.

My complete code:

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();
    }
}

I just ran the code, using:

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;
        }

    }
}

... Works fine for me. The list of movies are printed out to console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM