Skip to content

Instantly share code, notes, and snippets.

@mourawaldson
Created April 28, 2012 21:36
Show Gist options
  • Save mourawaldson/2522218 to your computer and use it in GitHub Desktop.
Save mourawaldson/2522218 to your computer and use it in GitHub Desktop.
Save file
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Salvar arquivo.
*
* @param filename
* @param input
* @throws IOException
*/
public static void saveFile(String filename, InputStream input)
throws IOException {
try {
if (filename == null)
filename = File.createTempFile("file", ".out").getName();
// Do no overwrite existing file
File file = new File(filename);
for (int i = 0; file.exists(); i++)
file = new File(filename + i);
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedInputStream bis = new BufferedInputStream(input);
int aByte;
while ((aByte = bis.read()) != -1)
bos.write(aByte);
bos.flush();
bos.close();
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment