29

I must use an existing method that is like saveAttachment(Attachment attachment) where Attachment has a File attribute.

My problem is that I'm retrieving a byte[] and I want to save it using this method. How can I have a "local" File just for saving ?

Sorry if my question is dumb, I don't know much about Files in Java.

2
  • 1
    From where you are retreiving byte[]? Where you want to save your data? Need more context & explanation Commented Sep 25, 2013 at 13:38
  • 1
    I save the data using a method that itself uses alfresco community sdk to store the file, so I can't change the way it is saved. And I retrieve the byte from a web-service that does not provide other information. For those reasons I didn't give more details in my question. Commented Sep 25, 2013 at 13:54

3 Answers 3

56
File tempFile = File.createTempFile(prefix, suffix, null);
FileOutputStream fos = new FileOutputStream(tempFile);
fos.write(byteArray);

Check out related docs:

File.createTempFile(prefix, suffix, directory);

Sign up to request clarification or add additional context in comments.

Comments

5

Reading All Bytes or Lines from a File

Path file = ...;
byte[] fileArray;
fileArray = Files.readAllBytes(file);

Writing All Bytes or Lines to a File

Path file = ...;
byte[] buf = ...;
Files.write(file, buf);

Comments

3

You're in luck.

File.createTempFile(String prefix, String suffix)

Creates a file in the default temp directory of the OS, where it's guaranteed you can write to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.