提问人:Harsh Sharma 提问时间:2/20/2020 最后编辑:donquih0teHarsh Sharma 更新时间:2/20/2020 访问量:70
如何使用 Scanner 类从文件中分别读取多行?
How to read multiple lines separately using Scanner class from a file?
问:
我在计算机上保存了一个名称为 Playlist.txt 的文本文件,并在其中保存了两首歌曲路径。如果只有一首歌,它工作得很好,它播放得很好。但是当我添加超过 1 首歌曲路径时,它正在读取最近添加的路径,即添加的最后一首歌曲路径。那么我怎样才能让 Scanner 对象分别读取行,它应该同时播放两首歌曲。
if (ae.getSource() == open_Playlist) {
fc = new JFileChooser();
int i = fc.showOpenDialog(null);
if (i == JFileChooser.APPROVE_OPTION) {
filepath = fc.getSelectedFile().getPath();
try {
File playList_File = new File(fc.getSelectedFile().getPath());
Scanner sc = new Scanner(playList_File);
while (sc.hasNextLine()) {
String data = sc.nextLine();
mp3 = new MP3Player(new File(data));
mp3.setShuffle(true);
System.out.println(data);
}
sc.close();
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(rootPane, e);
}
}
}
答: 暂无答案
评论
MP3Player
MP3Player
MP3Player
MP3Player
MP3Player