0

Is it possible to copy the contents of a file to a container then delete the file instantaneously, so in essence I am taking a snapshot of the file but not storing the contents in another temp file but rather in a java object

Pseudocode/code of what I am trying to accomplish

final File localPushFile = new File("testfile.txt");

if(!(localPushFile.exists())
{
   FileReader filestream = new FileReader(localPushFile);
   Object a  = filestream      //object variable will store snapshot of the file I am trying to copy
   deletefile(localPushFile)  //method that will delete the file as soon as a snapshot is taken
}

I am assuming I cant use BufferedReader as that just acts as a temp buffer?

lboyel
  • 1,930
  • 4
  • 27
  • 37
  • 1
    Can you describe the problem that caused you to seek such a solution? This sounds like an xy problem to me. – Andreas Oct 22 '14 at 21:37
  • I am waiting for a file, when I get it I immediately should capture its content and delete the file right away – lboyel Oct 23 '14 at 17:25
  • 1
    How about, instead of reading and deleting the file (which could use a lot of memory and would cause data loss if the process crashes), we copy the file into a temp directory with a timestamp appended to the filename? Would you be interested in such a solution? – Andreas Oct 23 '14 at 18:23
  • yes I am aware of that solution, but I am curious if I can try the other approach I illustrated in my question, for instance in this question http://stackoverflow.com/questions/326390/how-to-create-a-java-string-from-the-contents-of-a-file?rq=1 the person is asking how to store the file contents in a string, which is a possible way to achieve what I am trying to accomplish. My question is there any other way to do this like a built java object, method or library for this purpose. – lboyel Oct 23 '14 at 19:15
  • 1
    The simplest way to store a file's contents in memory is `byte[]`, a byte array. I'll post an answer describing this route, but I'll have to include a disclaimer that data *will* eventually be lost when the thread dies at an inopportune moment, and that the process *will* fail as the size of the file increases. – Andreas Oct 23 '14 at 19:56

2 Answers2

1

Yes, it is possible. However, to prevent errors and random problems, you need to read the entire file from the stream and then close the stream before deleting it.

ced-b
  • 3,957
  • 1
  • 27
  • 39
  • what holds the file being stored? – lboyel Oct 23 '14 at 17:24
  • 1
    Depends, on what your file contains. If it is text then you can store it in a string. If it is some other sort of data you could stick it into a byte array as Andreas suggests. If you have control over the program that creates the file, you could serialize a Java Object to file and deserialize it on the other end. – ced-b Oct 23 '14 at 21:18
1

Here is an example of reading the contents of a file into a byte array then deleting the file.

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

public class ReadFileIntoByteArray {
    public static byte[] getContentsAndDeleteFile(File file) throws IOException {
        byte[] data = Files.readAllBytes(file.toPath());
        file.delete();
        return data;
    }
}

Note: data will be lost when the thread dies at an inopportune moment, and that the process will fail as the size of the file increases

Andreas
  • 4,937
  • 2
  • 25
  • 35