VB and Java – Performing File-System Operations

Programmer
Programmer
Image by 200 Degrees from Pixabay

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.

VB and Java - 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:

VB

Dim fs as FileSystemObject
Set fs = New FileSystemObject

Java

File f;

VB

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")

Java

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();

VB

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")

Java

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();

VB

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")

Java

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();

VB

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")

Java

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();

VB

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 ‘GetFolder’ function in ‘fileSystemObject’. 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"

Java

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));

VB

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"

Java

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));

VB

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

Java

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 < fs.length; i++)
(processing here)
}

Related Posts...
Learn different object changes from VB 6 to VB.NET in this article.
Exception handling is a necessary element of coding. Compare the process and methods of exception
Want to learn networking programming techniques? This article describes the differences and approaches of network
Database Access is a very important aspect of programming. Learn how to do write database
Object serialization is useful for transfer of data across heterogeneous protocols. Learn its implementation in
Do you want to implement control arrays in VB.NET despite of lack of its native
The first step in learning any coding language is to learn its basic syntax rules.
Without reporting, any data centric program is incomplete. Learn how to use crystal reports (one