Skip to content

VB and Java – Performing File-System Operations

October 8, 2005

If you are a beginner programmer, then after reading the title of the page, the question must be arising in your mind that what is a ‘File-System Operation’.

Well, ‘File-System Operation’ is an operation which we do everyday on our PC but through our OS (Operating System). File-system operations include creating, renaming, and deleting files and folders on the disk with some other vital and important operations.

Normally, we do these tasks through the OS. But it is a definite need of any application to create, rename, delete files and folders and do many other file-system operations. In this article, we are going to focus on how to implement file-system operations in VB and JAVA.

Preliminary Introduction in VB and JAVA

In JAVA, File-System Operations are carried out through a ‘File‘ class. Before you start writing operational code, it is necessary that you initialize a ‘File‘ class. The ‘File‘ class is initialized as follows:

File f;
f=new File();

(Pls. note that you have to import package java.io in order to use the ‘File‘ class)

Now we come to VB. VB directly doesn’t support File-System operations. You will have to add a reference to ‘Microsoft Scripting Runtime‘ object before writing the actual code. The object reference is added as follows:

  1. Select Project >> References
  2. Select ‘Microsoft Scripting Runtime’ and put a checkbox in front of it.
  3. Press OK.

For your convenience, below is a picture of VB IDE on how to add references.

File-System Operations

File-System Operations

Once you add a reference, you need to initialize this object as follows:

Dim fs as FileSystemObject
Set fs=New FileSystemObject

Now, you are ready to perform the system operations. Below is the detailed table which specifies head-to-head comparison on how to perform these operations in both the languages:

Task VB Java
Object Initialization Dim fs as FileSystemObject
Set fs=New FileSystemObject
File f;
Creating Folder s.CreateFolder (folderPath as String)
folderpath will be the actual path of the folder that you want to create. For e.g. For creating a folder named 'MyFolder' in drive D, you would call this function as:

fs.CreateFolder("D:/MyFolder")
f=new File(String folderPath);
f.mkdir();
folderpath will be the actual path of the folder that you want to create. For e.g. For creating a folder named 'MyFolder' in drive D, you would call this function as:
f=new File("D:/MyFolder");
f.mkdir();
Creating a File fs.CreateTextFile (filePath as String)
filepath will be the actual path of the file that you want to create. For e.g. For creating a file named 'MyFile.txt' in drive D, you would call this function as:
fs.CreateTextFile("D:/MyFile.txt")
f=new File(String filePath);
f.createNewFile();
filepath will be the actual path of the file that you want to create. For e.g. For creating a file named 'MyFile.txt' in drive D, you would call this function as:
f=new File("D:/MyFile.txt");
f.createNewFile();
Deleting a Folder fs.DeleteFolder (folderPath as String)
folderpath will be the actual path of the folder that you want to delete. For e.g. For deleting a folder named 'MyFolder' in drive D, you would call this function as:
fs.DeleteFolder("D:/MyFolder")
f=new File(String folderpath);
f.delete();
folderpath will be the actual path of the folder that you want to delete. For e.g. For deleting a folder named 'MyFolder' in drive D, you would call this function as:
f=new File("D:/MyFolder");
f.delete();
Deleting a File fs.DeleteFile(filePath as String)
filepath will be the actual path of the file that you want to delete. For e.g. For deleting a file named 'MyFile.txt' in drive D, you would call this function as:
fs.DeleteFile("D:/MyFile.txt")
f=new File(String filePath);
f.delete();
filepath will be the actual path of the file that you want to delete. For e.g. For deleting a file named 'MyFile.txt' in drive D, you would call this function as:
f=new File("D:/MyFile.txt");
f.delete();
Renaming a Folder Dim f As Folder
Set f = fs.GetFolder(oldFolderPath as String)
f.Name = newFolderPath as String
oldFolderpath will be the path of the old folder that you want to rename. You will have to initialize a folder object here and then set it's pointer to an existing folder on the disk through 'fileSystemObject' 'GetFolder' function. For e.g. for renaming a folder named 'MyFolder' to 'NewFolder' that exists in drive D, you would call this function as:
Dim f As Folder
Set f = fs.GetFolder("D:/MyFolder"))
f.Name = "newFolder"
f=new File(String oldFolderPath);
f.renameTo(new File(String newFolderPath));
oldFolderpath will be the path of the old folder that you want to rename. You will have to initialize another File object as a parameter to specify a new name for the folder. For e.g. For renaming a folder named 'MyFolder' to 'NewFolder' that exists in drive D, you would call this function as:
f=new File("D:/MyFolder);
f.renameTo(new File("D:/NewFolder));
Renaming a File Dim f As File
Set f = fs.GetFile(oldFilePath as String)
f.Name = newFilePath as String
oldFilepath will be the path of the old file that you want to rename. You will have to initialize a file object here and then set it's pointer to an existing file on the disk through 'fileSystemObject' 'GetFile' function. For e.g. For renaming a file named 'MyFile.txt' to 'NewFile.txt' that exists in drive D, you would call this function as:
Dim f As File
Set f = fs.GetFile("D:/MyFile.txt"))
f.Name = "NewFile.txt"
f=new File(String oldFilePath);
f.renameTo(new File(String newFilePath));
oldFilePath will be the path of the old file that you want to rename. You will have to initialize another File object as a parameter to specify a new name for the file. For e.g. For renaming a file named 'MyFile.txt' to 'NewFile.txt' that exists in drive D, you would call this function as:
f=new File("D:/MyFile.txt);
f.renameTo(new File("D:/NewFile.txt));
Listing files in a folder Dim f As File
Dim fss As Files
Set fss = fs.GetFolder(folderP as String).Files
folderP will be the path of the old folder whose files you want to list. You will have to initialize a 'Files' object here and then use the 'folder' objects's 'Files' function to set it's pointer to a collection of existing file in that folder. For e.g. For listing files in a folder named 'MyFolder' that exists in drive D, you would call this function as:
Dim f As File
Dim fss As Files
Set fss = fs.GetFolder("D:/MyFolder").Files
Now you can use the For..Each.. Loop to process each File. For e.g.
For each f in fss

(processing here)

Next

File fs[];
File f;
f=new File(String oldFolderPath);
fs=f.listFiles();
oldFolderPath will be the path of the old folder whose files you want to list. You will have to initialize an array of 'File' object here and then use the 'File' objects's 'listFiles()' method to list all the files in that folder. For e.g. For listing files in a folder named 'MyFolder' that exists in drive D, you would call this function as:
File fs[];
File f;
f=new File("D:/MyFolder");
fs=f.listFiles();
Now you can use the For Loop to process each File. For e.g.
for(int i=0;i

(processing here)

}

Comments

Tell us what do you think.

There are no comments on this entry.

Trackbacks

Websites mentioned my entry.

There are no trackbacks on this entry

Add a Comment

Fill in the form and submit.