Solved

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

Posted on 2004-03-29
11
230 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 41
Windows 10 IE Certificate Issue 10 45
Way to decrease size of apk file 9 67
SHA2 certs for IIS AND Java? 2 88
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

773 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