Share via


Azure File Share code samples using Java version 8 client libraries

This article shows code samples that use version 8 of the Azure File Share client library for Java.

On March 31, 2023, we retired support for Azure SDK libraries which do not conform to the current Azure SDK guidelines. The new Azure SDK libraries are updated regularly to drive consistent experiences and strengthen your security posture. It's recommended that you transition to the new Azure SDK libraries to take advantage of the new capabilities and critical security updates.

Although the older libraries can still be used beyond March 31, 2023, they'll no longer receive official support and updates from Microsoft. For more information, see the support retirement announcement.

Prerequisites

To use the Azure File Share client library, add the following import directives:

// Include the following imports to use Azure Files APIs v11
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.file.*;

Access an Azure file share

Related article: Develop for Azure Files with Java

To access your storage account, use the CloudStorageAccount object, passing the connection string to its parse method.

// Use the CloudStorageAccount object to connect to your storage account
try {
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
} catch (InvalidKeyException invalidKey) {
    // Handle the exception
}

CloudStorageAccount.parse throws an InvalidKeyException so you'll need to put it inside a try/catch block.

Create a file share

Related article: Develop for Azure Files with Java

All files and directories in Azure Files are stored in a container called a share.

To obtain access to a share and its contents, create an Azure Files client. The following code example shows how to create a file share:

// Create the Azure Files client.
CloudFileClient fileClient = storageAccount.createCloudFileClient();

Using the Azure Files client, you can then obtain a reference to a share.

// Get a reference to the file share
CloudFileShare share = fileClient.getShareReference("sampleshare");

To actually create the share, use the createIfNotExists method of the CloudFileShare object.

if (share.createIfNotExists()) {
    System.out.println("New share created");
}

At this point, share holds a reference to a share named sample share.

Delete a file share

Related article: Develop for Azure Files with Java

The following sample code deletes a file share.

Delete a share by calling the deleteIfExists method on a CloudFileShare object.

try
{
    // Retrieve storage account from connection-string.
    CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);

    // Create the file client.
   CloudFileClient fileClient = storageAccount.createCloudFileClient();

   // Get a reference to the file share
   CloudFileShare share = fileClient.getShareReference("sampleshare");

   if (share.deleteIfExists()) {
       System.out.println("sampleshare deleted");
   }
} catch (Exception e) {
    e.printStackTrace();
}

Create a directory

Related article: Develop for Azure Files with Java

You can organize storage by putting files inside subdirectories instead of having all of them in the root directory.

The following code creates a subdirectory named sampledir under the root directory:

//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

//Get a reference to the sampledir directory
CloudFileDirectory sampleDir = rootDir.getDirectoryReference("sampledir");

if (sampleDir.createIfNotExists()) {
    System.out.println("sampledir created");
} else {
    System.out.println("sampledir already exists");
}

Delete a directory

Related article: Develop for Azure Files with Java

The following code example shows how to delete a directory. You can't delete a directory that still contains files or subdirectories.

// Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

// Get a reference to the directory you want to delete
CloudFileDirectory containerDir = rootDir.getDirectoryReference("sampledir");

// Delete the directory
if ( containerDir.deleteIfExists() ) {
    System.out.println("Directory deleted");
}

Enumerate files and directories in an Azure file share

Related article: Develop for Azure Files with Java

Get a list of files and directories by calling listFilesAndDirectories on a CloudFileDirectory reference. The method returns a list of ListFileItem objects on which you can iterate.

The following code lists files and directories inside the root directory:

//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

for ( ListFileItem fileItem : rootDir.listFilesAndDirectories() ) {
    System.out.println(fileItem.getUri());
}

Upload a file

Related article: Develop for Azure Files with Java

Get a reference to the directory where the file will be uploaded by calling the getRootDirectoryReference method on the share object.

//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

Now that you have a reference to the root directory of the share, you can upload a file onto it using the following code:

// Define the path to a local file.
final String filePath = "C:\\temp\\Readme.txt";

CloudFile cloudFile = rootDir.getFileReference("Readme.txt");
cloudFile.uploadFromFile(filePath);

Download a file

Related article: Develop for Azure Files with Java

The following example downloads SampleFile.txt and displays its contents:

//Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

//Get a reference to the directory that contains the file
CloudFileDirectory sampleDir = rootDir.getDirectoryReference("sampledir");

//Get a reference to the file you want to download
CloudFile file = sampleDir.getFileReference("SampleFile.txt");

//Write the contents of the file to the console.
System.out.println(file.downloadText());

Delete a file

Related article: Develop for Azure Files with Java

The following code deletes a file named SampleFile.txt stored inside a directory named sampledir:

// Get a reference to the root directory for the share.
CloudFileDirectory rootDir = share.getRootDirectoryReference();

// Get a reference to the directory where the file to be deleted is in
CloudFileDirectory containerDir = rootDir.getDirectoryReference("sampledir");

String filename = "SampleFile.txt"
CloudFile file;

file = containerDir.getFileReference(filename)
if ( file.deleteIfExists() ) {
    System.out.println(filename + " was deleted");
}