Solved

File and Vector

Posted on 2004-09-07
17
275 Views
Last Modified: 2010-03-31
I have a vector storing many FILE objects. How can I print out a String[] of file names of those stored in the vector.
0
Comment
Question by:jtcy
  • 7
  • 6
  • 2
  • +1
17 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11994411
Vector files = ...
String[] names = new String[files.length()];
for (int i=0; i<files.size(); i++)
{
   File f = (File) files.get(i);
   names[i] = f.getName();
   System.out.println(names[i]);
}
0
 
LVL 92

Accepted Solution

by:
objects earned 20 total points
ID: 11994419
woops, that should be:

Vector files = ...
String[] names = new String[files.size()];
for (int i=0; i<files.size(); i++)
{
   File f = (File) files.get(i);
   names[i] = f.getName();
   System.out.println(names[i]);
}
0
 

Author Comment

by:jtcy
ID: 11994526
Actually, the objects stored in the vector are FileWriter.....
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11994565
>Actually, the objects stored in the vector are FileWriter.....

First you must change the casting:

FileWriter f = (FileWriter) files.get(i);
0
 

Author Comment

by:jtcy
ID: 11994571
i know, but what method of FileWriter can print the name
0
 
LVL 12

Expert Comment

by:Giant2
ID: 11994601
try to use getEncoding method, but the Writer (and the Stream) must be open.


public String getEncoding()Return the name of the character encoding being used by this stream.
If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the OutputStreamWriter(OutputStream, String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.
0
 
LVL 92

Expert Comment

by:objects
ID: 11994619
> i know, but what method of FileWriter can print the name

you can't. The FileWriter is only used to write to the file, it doesn't know what the name of the file is.
0
 

Author Comment

by:jtcy
ID: 11994652
then..hm~~~~~........actually, it;s sort of not the real File name though, becuz i had this:

FileWriter writer = new FileWriter(filename);
aVector.add(writer);

so, all i really want is the "filename" field, sort of the variable name of the writer. Anyhow?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 92

Expert Comment

by:objects
ID: 11994670
you could add your filename to list when you create the writer:

FileWriter writer = new FileWriter(filename);
aVector.add(writer);
names.add(filename);

0
 

Author Comment

by:jtcy
ID: 11994678
Oh, how blur i was.
0
 

Author Comment

by:jtcy
ID: 11994687
But in another method, i need to obtain the file by a parameter of filename. Still, need to deal with that. :(

ie:

    public String[] getTranscript(String filename)
     {
       
     }
0
 
LVL 92

Expert Comment

by:objects
ID: 11994692
Not sure I follow, whats exactly is you need?
0
 

Author Comment

by:jtcy
ID: 11994824
I need to call getTranscript("filename"); and getTranscript will look thru the vector and find the corresponding filewriter and get the content of the filewriter to an array and return.
0
 
LVL 2

Expert Comment

by:anand29
ID: 11994829
If I am correct then I think  you want to store the named FileWriters in a vector and then you want to search those FileWriters by providing the name.
If the above requirement is correct then I think this might work please see the test code below just compile it and run.

You have to provide name of the file writer as name when you run the test code.
For testing I have added five FileWriters in vector.The names are one,two,three,four,five ...
So you can run the test code as

"java test one" or "java test two" ....etc

If the FileWrite with the  given name is found it will return the instance or it will return null.

import java.io.*;
import java.util.*;
public class test {


        Vector writers = new Vector();

        test(){
                try{
                        MyFileWriter w1 = new MyFileWriter("one");
                        MyFileWriter w2 = new MyFileWriter("two");
                        MyFileWriter w3 = new MyFileWriter("three");
                        MyFileWriter w4 = new MyFileWriter("four");
                        MyFileWriter w5 = new MyFileWriter("five");

                        writers.addElement(w1);
                        writers.addElement(w2);
                        writers.addElement(w3);
                        writers.addElement(w4);
                        writers.addElement(w5);
                }
                catch(IOException e){e.printStackTrace();}
        }

        MyFileWriter findByName(String name){
                for(int i=0;i<writers.size();i++){
                        MyFileWriter temp = (MyFileWriter)writers.elementAt(i);
                        if(temp.getName().equals(name)){
                                return temp;
                        }
                }
                return null;
        }
        public static void main(String str[]){
                String name = str[0];
                test t = new test();
                System.out.println(t.findByName(name));
        }
}

class MyFileWriter extends FileWriter{
        private String name;
        MyFileWriter(String name) throws IOException{
                super(name);
                this.name = name;
        }
        String getName(){
                return name;
        }

}
0
 

Author Comment

by:jtcy
ID: 11994847
Hm....I don think FileWriter has a method called getName....and if u mean i need to write another class called MyFileWriter to do that....I would do smth else.

What about, i don create FileWriter but create a File, do i do:

File aFile = new File(filename);
FileWriter writer = new FileWriter(aFile)
PrintWriter out = new PrintWriter(writer);
out.println("...");

Is this the way how i write to the file?
0
 
LVL 92

Expert Comment

by:objects
ID: 11994906
> and getTranscript will look thru the vector and find the corresponding filewriter and get the content of the filewriter to an array and return.

you could create Map of FileWriter's keyed on name.
To get the FIleWriter you would then use:

FileWriter writer = (FileWriter) map.get(filename);

> Is this the way how i write to the file?

yes
0
 
LVL 2

Expert Comment

by:anand29
ID: 11995132
Well if you extend the FileWriter and write the MyFileWrite class then the name can be contained inside the writer itself. And later if you need some other property than name that can also be contained inside the writer itseld.
Which makes sense. Right!!



0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.

746 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

17 Experts available now in Live!

Get 1:1 Help Now