Java Utililty Methods FileOutputStream Write

List of utility methods to do FileOutputStream Write

Description

The list of methods to do FileOutputStream Write are organized into topic(s).

Method

voidcopyFile(InputStream in, File destination)
copy File
try (FileOutputStream out = new FileOutputStream(destination)) {
    byte[] bytes = new byte[65535];
    int cnt;
    while ((cnt = in.read(bytes)) > 0) {
        out.write(bytes, 0, cnt);
voidcopyFile(InputStream in, File destination)
copy File
if (!destination.canWrite())
    throw new Exception("Creating of temporary file for lens failed. Destination file "
            + destination.getAbsolutePath() + " is not accessible.");
OutputStream out = new FileOutputStream(destination);
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
    out.write(buf, 0, length);
...
voidcopyFile(InputStream input, File outFile)
Copy an InputStream to a file.
OutputStream os = new FileOutputStream(outFile);
try {
    byte[] buffer = new byte[COPY_BUFFER_SIZE];
    int readLen;
    while ((readLen = input.read(buffer)) != -1) {
        os.write(buffer, 0, readLen);
} finally {
...
FilecopyFile(InputStream input, File outputLocation)
copy File
if (!outputLocation.exists()) {
    outputLocation.createNewFile();
FileOutputStream dest = new FileOutputStream(outputLocation);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = input.read(bytes)) != -1) {
    dest.write(bytes, 0, read);
...
longcopyFile(InputStream input, OutputStream output)
copy File
byte buffer[] = new byte[2048];
long count = 0;
int n = 0;
while (-1 != (n = input.read(buffer))) {
    output.write(buffer, 0, n);
    count += n;
return count;
...
voidcopyFile(InputStream sourceFileIs, File destDirFile, String destFileName)
copy File
int c;
StringBuffer fileSb = new StringBuffer();
while ((c = sourceFileIs.read()) != -1) {
    fileSb.append((char) c);
String fileStr = fileSb.toString();
File f = new File(destDirFile, destFileName);
FileOutputStream fos = new FileOutputStream(f);
...
voidcopyFileFromAssets(InputStream inputStream, String pathToWrite)
copy File From Assets
OutputStream outputStream = null;
try {
    File newFile = new File(pathToWrite);
    if (!newFile.exists()) {
        newFile.createNewFile();
    outputStream = new FileOutputStream(new File(pathToWrite));
    int read = 0;
...
voidcopyFileUsingFileStreams(InputStream source, File dest)
copy File Using File Streams
OutputStream output = null;
try {
    output = new FileOutputStream(dest);
    byte[] buf = new byte[1024];
    int bytesRead;
    while ((bytesRead = source.read(buf)) > 0) {
        output.write(buf, 0, bytesRead);
} finally {
    try {
        source.close();
        output.close();
    } catch (Exception ex) {
        ex.printStackTrace();
voidcopyFileUsingStream(InputStream sourceStream, File dest)
Copy file from a given InputStream to a destination file
if (sourceStream == null) {
    throw new IllegalArgumentException("Source stream is null");
OutputStream destStream = null;
try {
    destStream = new FileOutputStream(dest);
    byte[] buffer = new byte[1024];
    int length;
...
voidsave(byte[] bs, String fn)
save
new File(fn).getParentFile().mkdirs();
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fn));
out.write(bs);
out.close();