我目前正在尝试弄清楚如何.txt
在 Java 中读取文件并将其保存到动态数组中,但我不知道如何将读取的.txt
文件保存到数组中。我要读取的文件名为songCollection.txt
.
具体的数据部分需要是:
title,artist,genre,album,songID
以下是我当前的代码,任何帮助将不胜感激。谢谢
代码:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.io.*;
public class song {
private int SongID; // The unique song identifier
private String title; // The song title
private String artist; // The song artist
private String genre; // The genre of the song
private String album; // The album name
private String songData;
public song() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileInputStream fstream = new FileInputStream("songCollection.txt");
// use DataInputStream to read binary NOT text
// DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
String[] splitOut = strLine.split(", ");
for (String token : splitOut)
System.out.println(token);
}
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
Readable fileSong;
String[] songData = new Scanner(fileSong);
while (songData.hasNextLine()) {
String songCollection = songData.nextLine();
songData = songCollection.split(",");
}
}
}