将包含多个元素的文本文件读入arraylist(set and get)

时间:2016-04-26 08:48:36

标签: java arraylist text-files readfile

我正在尝试从类型的文本文件中读取(艺术家,标题,流派,唱片公司,发行年份,歌曲数量,播放时间):

The Beatles,Abbey Road,Rock,Apple Records,1969,17,47.16

Sia,1000种形式的恐惧,流行音乐,Intertia,2014,12,48.41

Taylor Swift,现在发言

我创建了一个CD类:

package q;

public class CD {
  // 
  private String artist;
  private String titel;
  private String genre;
  private String recordcompany;
  private int year; // 
  private int songs; // 
  private double playtime; // 


  public CD() { // 
  }
  public CD(String newArtist, String newTitel) {
    artist = newArtist;
    titel = newTitel;
  }
  public CD(String newArtist, String newTitel, String newGenre, String newRecordcompany, int newYear, int newSongs, double newPlaytime) {
    artist = newArtist;
    titel = newTitel;
    genre = newGenre;
    recordcompany = newRecordcompany;
    year = newYear;
    songs = newSongs;
    playtime = newPlaytime;
  }

  public String getArtist() { //  
    return artist;
  }
  public String getTitel() {
    return titel;
  }
  public String getGenre() {
    return genre;
  }
  public String getRecordcompany() {
    return recordcompany;
  }
  public int getYear() {
    return year;
  }
  public int getsong() {
    return songs;
  }
  public double getPlaytime() {
    return playtime;
  }
  public void setArtist(String newArtist) { // 
    artist = newArtist;
  }
  public void setTitel(String newTitel) {
    titel = newTitel;
  }
  public void setGenre(String newGenre) {
    genre = newGenre;
  }
  public void setRecordcompany(String newRecordcompany) {
    recordcompany = newRecordcompany;
  }
  public void setYear(int newYear) {
    year = newYear;
  }
  public void setSongs(int newSongs) {
    songs = newSongs;
  }
  public void setplaytime(double newPlaytime) {
    playtime = newPlaytime;
  }

  @
  Override public String toString() { // 
    return ("Artist " + artist + System.lineSeparator() + "Titel: " + titel + System.lineSeparator() + "Genre: " + genre + System.lineSeparator() + "Recordcompany: " + recordcompany + System.lineSeparator() + "Year: " + year + System.lineSeparator() + "Songs: " + songs + System.lineSeparator() + "Playtime: " + playtime + System.lineSeparator());
  }
}

我正在尝试从文本文件中读取,然后将其转换为arraylist。我知道我已经过度复杂了,首先将文本文件转换为字符串数组,然后将其转换为arraylist。我想在创建数组时使用set和get方法,如果可能的话。我想知道你是否有任何关于我如何使代码变得更简单并且如果可能包括set和get方法的提示,谢谢。

package q;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


public class Q {


  public static void main(String[] args) throws FileNotFoundException {


    String artist = "";
    String titel = "";
    String genre = "";
    String recordcompany = "";
    String year = "";
    String songs = "";
    String playtime = "";


    try {
      BufferedReader br = new BufferedReader(new FileReader("nej.txt"));
      String line = null;

      while ((line = br.readLine()) != null) {
        String tmp[] = line.split(",");
        artist = tmp[0];
        titel = tmp[1];

        if (tmp.length > 2) {
          genre = tmp[2];
          recordcompany = tmp[3];
          year = (tmp[4]);
          songs = (tmp[5]);
          playtime = (tmp[6]);
        } else {
          genre = "";
          recordcompany = "";
          year = "";
          songs = "";
          playtime = "";

        }
        List < String > unsorted = Arrays.asList(tmp);
        for (String e: unsorted) {
          System.out.println(e);
        }
      }

      br.close();
    } catch (IOException e) {
      System.out.println(e);
    }
  }
}

EDITED

package q;

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 Q {


  public static void main(String[] args) throws FileNotFoundException {


    String artist = "";
    String titel = "";
    String genre = "";
    String recordcompany = "";
    int year = 0;
    int songs = 0;
    double playtime = 0.0;



    try {
      BufferedReader br = new BufferedReader(new FileReader("nej.txt"));
      String line = null;

      List < CD > cdsList = new ArrayList < > ();

      while ((line = br.readLine()) != null) {
        CD cd = new CD();
        String tmp[] = line.split(",");
        cd.setArtist(tmp[0]);
        cd.setTitel(tmp[1]);

        if (tmp.length > 2) {
          cd.setGenre(tmp[2]);
          cd.setRecordcompany(tmp[3]);
          cd.setYear(Integer.parseInt(tmp[4].trim()));
          cd.setSongs(Integer.parseInt(tmp[5].trim()));
          cd.setplaytime(Double.parseDouble(tmp[6].trim()));

        }
        System.out.println(cdsList);
      }

      br.close();

    } catch (IOException e) {
      System.out.println(e);
    }

  }
}

1 个答案:

答案 0 :(得分:0)

如下所示。

public static void main(String[] args) {
        try {
          BufferedReader br = new BufferedReader(new FileReader("nej.txt"));
          String line = null;

          List < CD > cdsList = new ArrayList < > ();

          while ((line = br.readLine()) != null) {
            CD cd = new CD();
            String tmp[] = line.split(",");
            cd.setArtist(tmp[0]);
            cd.setTitel(tmp[1]);

            if (tmp.length > 2) {
              cd.setGenre(tmp[2]);
              cd.setRecordcompany(tmp[3]);
              cd.setYear(Integer.parseInt(tmp[4].trim()));
              cd.setSongs(Integer.parseInt(tmp[5].trim()));
              cd.setplaytime(Double.parseDouble(tmp[6].trim()));

            }
            cdsList.add(cd);

          }
          System.out.println(cdsList);

          br.close();

        } catch (IOException e) {
          System.out.println(e);
        }

}