Solved

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

Posted on 2004-03-29
11
235 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 40
oracle 11g 23 107
Way to decrease size of apk file 9 87
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 30
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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

838 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