Reputation:
I'm getting a file from a HTTP request and I need to read and search for a string.
My problem is that the file is not on the system so I need to transferTo
a created file.
My question is: where do usually hosts create this temporary files? I want to read the file, search for how many times a string appears and delete the file? Is this the way things are done?
public static int readTestCaseCode(MultipartFile multipartFile) {
File file = new File(//Do I need to specify a location?)
multipartFile.transferTo();
Scanner scanner = new Scanner(file)
}
Upvotes: 0
Views: 63
Reputation: 19926
Use the static factory method File#createTempFile(String, String)
which exactly creates what the name says.
File file = File.createTempFile("myFile", ".exe");
You can also omit the ending and just pass null
, which then will be filled with ".tmp"
, e.g.
File file = File.createTempFile("myFile", null);
The file can then afterwards be deleted by invoking the File#delete()
method:
file.delete();
Upvotes: 2