user1513323
user1513323

Reputation: 51

Splitting a file in Java

To read file into a list, create a new class called ListUtils and write a method called public static ListElement readMP3List(String fileName) throws IOException which will take the name of the file to be read and return the head of a linked list containing the the objects that the file. In readMP3List, you should open fileName using the classes java.io.BufferedReader and java.io.FileReader and read one line at a time. Each line you should break apart into its fileName, artist, etc., and then use these values to populate a new MP3Info object. Then you make a new ListElement object containing the MP3Info object you just made and put it onto your list.

So far I've got:

public class ListUtils{

    public static ListElement readMP3List(String fileName) throws IOExeption{
    {
            // takes name of file  to be read
            // returns the head of Linkedlist
            File file = new File("random_sample.tsv");
            BufferedReader br = new BufferedReader(new FileReader(file));
            String first= br.readLine();
    }

The file has multiple lines of names,artist,etc in which I have to split to. I'm really confused on how to use the split(regex) in order to accomplish this.

An example of the file is this: fileName artist songName album trackNum numSeconds year genre \n

Upvotes: 0

Views: 879

Answers (2)

I think this is what you need:

String fist=  "fileName artist songName album trackNum numSeconds";

String[] data = fist.split(" ");//whitespace if tab replace with "\t"

String fileName = data[0];
String artist = data[1];
String songName = data[2];
String album = data[3];
String trackNum = data[4];
String numSeconds = data[4];

Upvotes: 1

cklab
cklab

Reputation: 3771

I'm going to go out on a limb and say this is homework? Anyway, here is a push in the right direction: given that your file seems to have the extension tsv, I can only assume your values are separated by tabs. For splitting by tabs, look into the using split(...) with \t.

Note: you're not using the fileName that is passed in :)

Upvotes: 2

Related Questions