Continuing on with the re-learning Java topic, seems I needed a small program that read the files in a directory and placed those file names and their full qualifying path name into a jTextField. So as this was being coded I thought, good time to add to that to a fileutils() class, later decided a fileUtilsWrite() class is needed too.
As things will have it, this fileUtils class will just return the filenames from the directory. As a prototype class, it is fairly instructive.This particular file does in fact NOT return the full qualifying path name of the files. That code went into yet another Netbeans project.
Writing this fileUtils class went pretty quickly. Most of that is a cut/paste from the Internet (why re-invent the wheel). The challenge was that the calling program, or the uitest.java (as it is called) needs to be able to read off the file list one by one. There were a few complications so I settled on reading in the files into a vector (vc), then using the object oriented nature to pick these file names off, one by one. This is how that is done:
System.out.println("uitest: " + fileUt.getSize());
for(int i=0;i<fileUt.getSize();i++){
System.out.println("uitest: " + fileUt.getFileName(i));
}
Here is the code for reading the directory and placing the filenames into a vector.
public class fileUtils {
public fileUtils(String szendsWith){
System.out.println("fileUtils:");
File folder = new File("c:/");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File: " + listOfFiles[i].getName());
}
else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory: " + listOfFiles[i].getName());
}
if(listOfFiles[i].getName().endsWith(szendsWith)){
System.out.println("File: " + listOfFiles[i].getName());
vc.add(listOfFiles[i].getName());
}
}
//System.out.println("fileUtils: " + vc.get(1));
}
public int getSize(){
size=vc.size();
return size;
}
public String getFileName(int i){
String fileName;
fileName=vc.get(i).toString();
return fileName;
}
private int size;
private String szFileName;
public Vector vc = new Vector();
}
But wait, there's more to come, it is the fileUtilsWrite() class.
No comments:
Post a Comment