Solved

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

Posted on 2004-03-29
11
221 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: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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Eclipse Neon and jdk 1.8.0 11 138
groovy example issue 10 90
javap bin 2 28
table example 4 24
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 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.

895 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