My co-workers and I are having an argument about how the File.delete()
method works in Java.
In our code:
File outFile = new File("/dir/name.ext");
if(outFile.exists())
outFile.delete();
FileInputStream inStream = new FileInputStream(outFile);
WriteFile.writeFile(inStream); // Writes the actual file
I can't include the entire method body of writeFile
here for security reasons, but after it creates the database object needed, it performs the following action:
BufferedOutputStream out = null;
Object[] args = {"an_encrypted_data_clob_name_in_the_database"};
Class[] argTypes = {Class.forName("java.lang.String")};
Object result = WSCallHelper.jdbcCall(null, rs, "getCLOB", args, argTypes);
CLOB clob = (CLOB)result;
out = new BufferedOutputStream(clob.getAsciiOutputStream());
byte[] buffer = new byte[512];
int bytesRead = -1;
while((bytesRead = inStream.read(buffer)) > -1)
out.write(buffer, 0, bytesRead);
I know it's a little unclear, but the general gist of it is that it creates an AsciiOutputStream
of the Clob
(yes it's supposed to be a Clob
) and writes it to the inStream
object that is passed from the previous method.
They're convinced that this won't write to the file directory because of the File.delete();
method, but I know for a fact that there was a file in that location yesterday, and this code ran today and wrote a file in that exact location. Because, although the actual file is deleted, the pointer for where that file is located is still in outFile
, and the creation of inStream
with outFile
makes inStream
point to that location.
Is there any reason to believe that this file wouldn't be written in this scenario? Ideally, I'd like some proof that the delete()
method removes a file that the File
object points to, not the pointer itself.
File
only contains a file location; it doesn't maintain any sort of connection to the file, and won't keep a file alive. – PalaciosFile
is a poorly named class because aFile
instance does not represent (or in any way stand for) a file. AFile
instance represents a pathname. It should be obvious that deleting a file with a given pathname does not prevent you from subsequently creating a new file that has the same pathname. – Terrarium