I am currently trying to figure out how to read and save .txt
files to a dynamic array in Java, I do not know how to save the read .txt
file into the array. The file I am trying to read is named songCollection.txt
.
The specific data parts need to be:
title,artist,genre,album,songID
Below is my current code, any help will be much appreciated. Thanks
Code:
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(",");
}
}
}
I am not sure what exactly you are looking. But if you are simply looking to store data in then you can store string array in ArrayList or any collection that suits you. If you want to use it later for fast retrieval you can use any of the map implementation to store the key and value pair
Question: "I do not know how to save the read .txt file into the array"
Answer:
Most text files can be read into your program using a simple Scanner
. For example:
Scanner input = new Scanner(fileName);
int[] ints = new int[10];
int i = 0;
while (input.hasNextInt()) {
input.nextInt() = ints[i];
i++
}
input.close()
The only issue with your approch is that you don't know how big of an array you will need. I recommend you store your input in a data structure that dynamically allocates space, like a LinkedList
or an UnboundedStack
.
Instead of printing out tokens to console you should create a new Song instance and set values to it:
song s = new song();
s.SongId = Integer.parseInt(splitOut[0]);
s.title = splitOut[1];
s.artist = splitOut[2];
...
And then put this song instance to a list.
Also consider implementing Song constructor with all these fields as arguments.
String temp = "";
try{
Scanner input = new Scanner("yourfile.txt");
while(input.hasNext()){
temp = temp + "_" + input.next();
}
input.close();
}
catch(Exception e){
}
String fin[] = temp.split("_");
You should define the constructor of your Song
class as:
public Song(int songId, String title, String artist, String genre,
String album, String songData) {
this.songId = songId;
this.title = title;
this.artist = artist;
this.genre = genre;
this.album = album;
this.songData = songData;
}
And here's an example of using BufferedReader
to read all song lines into a list ( this code requires Java7 ):
List<Song> songs = new ArrayList<>(); // List of Song objects
try (BufferedReader input = new BufferedReader(new InputStreamReader(
new FileInputStream("songCollection.txt"), Charset.forName("UTF-8")))) {
String line;
while ((line = input.readLine()) != null) {
String[] arr = line.split(",");
songs.add(new Song(Integer.parseInt(arr[0]), arr[1], arr[2], arr[3],
arr[4], arr[5])); // <- Add new Song to list.
}
} catch (IOException e) {
e.printStackTrace();
}
Live example: http://ideone.com/HFn4jY
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.