Java 将包含多个元素的文本文件读入arraylist(设置和获取)

Java 将包含多个元素的文本文件读入arraylist(设置和获取),java,arraylist,text-files,readfile,Java,Arraylist,Text Files,Readfile,我正在尝试读取以下类型的文本文件(艺术家、标题、流派、唱片公司、发行年份、歌曲数量、播放时间): 披头士乐队,艾比路,摇滚,苹果唱片,1969,17,47.16 新航,1000种形式的恐惧,流行音乐,Intertia,2014,12,48.41 泰勒·斯威夫特,现在发言 我创建了一个CD类: q包; 公共类光盘{ // 私人弦乐演奏家; 私有字符串滴度; 私人弦乐体裁; 私人字符串记录公司; 私人国际年;// 私人int歌曲;// 私人双人游戏时间;// 公共CD(){// } 公共CD(S

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

披头士乐队,艾比路,摇滚,苹果唱片,1969,17,47.16

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

泰勒·斯威夫特,现在发言

我创建了一个CD类:

q包;
公共类光盘{
// 
私人弦乐演奏家;
私有字符串滴度;
私人弦乐体裁;
私人字符串记录公司;
私人国际年;//
私人int歌曲;//
私人双人游戏时间;//
公共CD(){//
}
公共CD(String newArtist、String Newitel){
艺术家=新艺术家;
滴度=newitel;
}
公共CD(弦乐新人、弦乐新人、弦乐新人、弦乐新人、弦乐新人公司、国际新年、国际新闻歌曲、双新播放时间){
艺术家=新艺术家;
滴度=newitel;
流派=新流派;
recordcompany=newRecordcompany;
年=新年;
歌曲=新闻歌曲;
playtime=新的playtime;
}
公共字符串getArtist(){//
回归艺术家;
}
公共字符串getitel(){
返回滴度;
}
公共字符串getgreen(){
回归体裁;
}
公共字符串getRecordcompany(){
返回记录公司;
}
公共int getYear(){
回归年;
}
公共int getsong(){
返回歌曲;
}
公共双getPlaytime(){
返回游戏时间;
}
公共void setArtist(字符串newArtist){//
艺术家=新艺术家;
}
公共无效集合(字符串新集合){
滴度=newitel;
}
public void setgreen(字符串newgreen){
流派=新流派;
}
public void setRecordcompany(字符串newRecordcompany){
recordcompany=newRecordcompany;
}
公共无效设置年(整数新年){
年=新年;
}
公共电台歌曲(国际新闻歌曲){
歌曲=新闻歌曲;
}
公共无效设置播放时间(双倍新播放时间){
playtime=新的playtime;
}
@
重写公共字符串toString(){//
return(“艺人”+Artist+System.lineSeparator()+”Titel:“+Titel+System.lineSeparator()+”流派:“+Genre+System.lineSeparator()+”唱片公司:“+Recordcompany+System.lineSeparator()+”年份:“+Year+System.lineSeparator()+”歌曲:“+Songs+System.lineSeparator()+”播放时间:“+Playtime+System.lineSeparator()”;
}
}
如下所示

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

}
publicstaticvoidmain(字符串[]args){
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(“nej.txt”);
字符串行=null;
ListcdsList=newarraylist<>();
而((line=br.readLine())!=null){
CD=新的CD();
字符串tmp[]=line.split(“,”);
cd.setArtist(tmp[0]);
cd.setTitel(tmp[1]);
如果(tmp.length>2){
cd.SETGREEN(tmp[2]);
cd.setRecordcompany(tmp[3]);
setYear(Integer.parseInt(tmp[4].trim());
cd.setSongs(Integer.parseInt(tmp[5].trim());
setplaytime(Double.parseDouble(tmp[6].trim());
}
cdsList.add(cd);
}
System.out.println(cdsList);
br.close();
}捕获(IOE异常){
系统输出打印ln(e);
}
}

您的文本文件是否包含所有记录的所有详细信息??比如:::披头士、艾比路、摇滚、苹果唱片,1969、17、47.16嘿,你直接使用数组分割并将其转换为列表,为什么你可以制作CD数组,在设置的同时你还可以放置if-else。希望这能有所帮助。@Abhishek有些记录不包含所有信息,有些记录只包含艺术家和标题。@KulbhushanSingh你能开发吗,我不完全理解。我想你需要修改你的数据以正确的格式。。。您的代码将无法分辨哪些数据与POJO中的哪个字段相对应…请不要忘记解析'year'、'songs'和'playtime'以转换为数字数据类型(int和double)@niyasc当我使用此方法(解析year、songs和playtime)时,我得到错误:线程“main”中出现异常java.lang.NumberFormatException:输入字符串:java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)java.lang.Integer.parseInt(Integer.java:569)java.lang.Integer.parseInt(Integer.java:615)将年份和歌曲解析为Integer.parseInt(字符串),将播放时间解析为Double.parseDouble(字符串)我认为在1969年,你有空白,以及添加修剪它。在解析之前,我将编辑answerJust
trim
空格。