Solved

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

Posted on 2004-03-29
11
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Mayank S
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
 
LVL 30

Expert Comment

by:Mayank S
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:Mayank S
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

735 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