Here's an example Java class that fulfills the requirements mentioned:
```java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
public class BankAccountData {
private List<String> accountData;
// Constructor to create an empty ArrayList of bank account data
public BankAccountData() {
accountData = new ArrayList<>();
}
// Method to clear all data from the list
public void clear() {
accountData.clear();
}
// Method to add an account holder to the list
public void addAccountHolder(String accountHolder) {
accountData.add(accountHolder);
}
// Method to write account holder information to a file with encryption
public boolean writeToFile(File file, String encryptionKey) {
try (PrintWriter writer = new PrintWriter(file)) {
for (String accountHolder : accountData) {
String encryptedData = encryptData(accountHolder, encryptionKey);
writer.println(encryptedData);
}
return true;
} catch (FileNotFoundException e) {
System.out.println("Error: File not found.");
return false;
}
}
// Helper method to encrypt the account holder data
private String encryptData(String data, String encryptionKey) {
// Implement your encryption algorithm here
// Example: Simple XOR encryption
StringBuilder encryptedData = new StringBuilder();
for (int i = 0; i < data.length(); i++) {
encryptedData.append((char) (data.charAt(i) ^ encryptionKey.charAt(i % encryptionKey.length())));
}
return encryptedData.toString();
}
}
```
In this class, we have a no-parameter constructor that initializes an empty ArrayList called `accountData`. The `clear()` method clears all data from the list. The `addAccountHolder()` method accepts an account holder as a parameter and adds it to the list. The `writeToFile()` method accepts a `File` reference and an encryption key. It writes each account holder's information, encrypted using the encryption key, to the file. It returns `true` if the writing of the file was successful, and `false` if a `FileNotFoundException` occurs.
Please note that the `encryptData()` method provided is a simple XOR encryption example and may not be suitable for secure encryption. You may need to implement a more secure encryption algorithm depending on your requirements.