I'm attempting to learn more about secure coding in Java, and was tasked with making my code more compliant. If dealing with sensitive file data in the example program I've created below how can I make sure that the data will not linger after execution in the memory?
I'm mainly wondering if there is a need to clear the memory at all with the program as is.
I have been reading multiple posts, however I have not found a straight forward explanation as of yet. If you would take a different approach to read a line from a file in a safer way please feel free to let me know. After reading so many sources I'm beginning to feel a little lost. Any help for a newcomer would be greatly appreciated!
import java.io.*;
public class Data {
private static final String file = "fileData.txt";
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(file));
String data = null;
try {
// Read from the file
data = br.readLine();
// closing BufferedReader
br.close();
// Printing a line from file
System.out.println(data);
}
//define catch to handle the IOException
catch (IOException e) {
e.printStackTrace(); // Print Stack Trace for further analysis
}
}
}