嘗試使用文本文件中的行打印陣列。打印從文本文件創建的陣列
文本文件:
Queen Greatest_Hits_I Rock 2011 UNI 17 58.19
Madonna Music Pop 2000 Mavericks_Records 10 44.40
Miles_Davies King_of_Blue Jazz 1959 Columbia_Records 6 45.44
Charles_Bradley Changes Soul 2016 Daptone_Records 11 40.44
David_Bowie Blackstar Rock 2016 ISO 7 41.13
Queen Queen_II
Prince Purple_Rain
代碼:
public static void main(String[] args) throws FileNotFoundException{
Scanner read = new Scanner(new File("C:\\CD.txt"));
int a=0,i=0;
while(read.hasNext()){
a++;
read.nextLine();
}read.close();
String [] l = new String [a];
read= new Scanner(new File("C:\\CD.txt"));
while(read.hasNext()){
String n =read.nextLine();
l[i]=n;
i++;
}
System.out.println(Arrays.toString(l));
}
代碼輸出:
[Queen Greatest_Hits_I Rock 2011 UNI 17 58.19, Madonna Music Pop 2000 Mavericks_Records 10 44.40, Miles_Davies King_of_Blue Jazz 1959 Columbia_Records 6 45.44, Charles_Bradley Changes Soul 2016 Daptone_Records 11 40.44, David_Bowie Blackstar Rock 2016 ISO 7 41.13, Queen Queen_II, Prince Purple_Rain]
輸出我想:
Queen Greatest_Hits_I Rock 2011 UNI 17 58.19
Madonna Music Pop 2000 Mavericks_Records 10 44.40
Miles_Davies King_of_Blue Jazz 1959 Columbia_Records 6 45.44
Charles_Bradley Changes Soul 2016 Daptone_Records 11 40.44
David_Bowie Blackstar Rock 2016 ISO 7 41.13
Queen Queen_II
Prince Purple_Rain
我在做什麼錯?
而不是將每行添加到數組,只需輸出該行。 –