Solved

I have the files How do I get them into ListOfFiles?

Posted on 2004-03-29
11
210 Views
Last Modified: 2010-03-31
I have this code to create a list of files. I have the files How do I get them into ListOfFiles? I have some ideas but I will wait for more experience to make some suggestions. Can you show me in code please.



ListOfFiles mylist = new ListOfFiles(args);
List streams = new ArrayList();
while (mylist.hasMoreElements())
{
   InputStream in = (InputStream) mylist.nextElement();
   streams.add(AudioSystem.getAudioInputStream(in));
}
SequenceInputStream s = new SequenceInputStream(format, streams);
0
Comment
Question by:Drop_of_Rain
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 10711459
Don't know exactly what you mean.
What is ListOfFiles? Your own type? Is it a List?

A List can contain any objects.
A File is an object.
So a List can contain Files.
0
 

Author Comment

by:Drop_of_Rain
ID: 10711478
0
 
LVL 37

Accepted Solution

by:
zzynx earned 200 total points
ID: 10711643
>> I have the files How do I get them into ListOfFiles?

Well, I think your ListOfFiles class simply need an add() function

public class ListOfFiles implements Enumeration {

    private List listOfFiles = new ArrayList(); // Start with an empty list by default
    private int current = 0;

    public ListOfFiles() {
    }
    public ListOfFiles(List listOfFiles) {
        this.listOfFiles = listOfFiles;
    }

    public boolean hasMoreElements() {
        if (current < listOfFiles.size())
            return true;
        else
            return false;
    }

    public Object nextElement() {
        InputStream in = null;
        if (!hasMoreElements())
            throw new NoSuchElementException("No more files.");
        else {
            String nextElement = (String)listOfFiles.get(current);
            current++;
            try {
                in = new FileInputStream(nextElement);
            } catch (FileNotFoundException e) {
                System.err.println("ListOfFiles: Can't open " + nextElement);
            }
        }
        return in;
    }

    public void add(String fileName) {
        listOfFiles.add(fileName);
    }
}

Remark that since you want your list to grow dynamically you better use List instead of arrays.
So, I changed the member listOfFiles from String[] to List.
I adapted the code of the existing functions accordingly and added the add() function.
I also added a new constructor, that takes no parameters.

Now, maybe you think: "But I have an array of files"! How do I populate my ListOfFiles object?
Since the 2nd constructor takes a List as parameter?
Two options:
1) Create you ListOfFile object with the 1st constructor and then iterate through your array and
use the add() function to populate your list.
2) Create from your array a List by using
        List lst = new ArrayList(Arrays.asList(yourArray));
    and pass it to the 2nd constructor.


0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10711651
Yes, so what's the problem?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10711692
>> Yes, so what's the problem?
?????
You say "yes" to what question?
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:mayankeagle
ID: 10711719
Your code was not there when I typed that ;-)

I wanted to hear some more details from Drop_of_rain.
0
 

Author Comment

by:Drop_of_Rain
ID: 10711798
I want to try the Enumeration process. I have to resolve this because this is a big part of my program. I made up some sample files to test it. I needed to be able to get the files into ListOfFiles because that is what Enumeration expects. ListOfFiles took its arguments from the command line. The code above was created to create that same list from an arraylist.

I think you have resolved this for me. I will wait for mayankeagle comments before I close.

Thanks
Christopher
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10711837
Well, if you're satisfied with zzynx's comments, then go ahead and close it :-)

Actually, yes, you do need an add () method to add the items.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10711843
>> I think you have resolved this for me.
Glad to hear that.

>> I will wait for mayankeagle comments before I close.
OK
0
 
LVL 9

Assisted Solution

by:mmuruganandam
mmuruganandam earned 100 total points
ID: 10712427
Great!  

Nothing with points, just a suggestion!  When you are using the Collection as your data structure, you can always get the Iterator from the List.

Iterator is a better option than enumeration for the only reason of "fail-fast".  Fail fast is nothing but, when you iterate your collection and some other thread trying to remove a value from your collection.  That time, your iterator will throw ConcurrentModificationException.  That way, it fails immediately to avoid future unexcepted failures.
0
 

Author Comment

by:Drop_of_Rain
ID: 10714792
Thanks you guys. I increased the points by 100 to give mmuruganandam some for his suggestion
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now