🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › how-to-write-to-a-file-in-java-using-fileoutputstream
How to write to a file in java using FileOutputStream
September 11, 2022 - To convert the String into an array of bytes, we are using getBytes() method of String class. import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileDemo { public static void main(String[] args) { FileOutputStream fos = null; File file; String mycontent = "This is my Data which needs" + " to be written into the file"; try { //Specify the file path here file = new File("C:/myfile.txt"); fos = new FileOutputStream(file); /* This logic will check whether the file * exists or not.
🌐
GeeksforGeeks
geeksforgeeks.org › java › fileoutputstream-in-java
FileOutputStream in Java - GeeksforGeeks
November 3, 2025 - FileOutputStream fos = new FileOutputStream( "output.txt"); ... Creates a file output stream to write to the file represented by the specified File object.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › FileOutputStream.html
FileOutputStream (Java Platform SE 7 )
A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other ...
🌐
ZetCode
zetcode.com › java › fileoutputstream
Java FileOutputStream - writing to files in Java
January 27, 2024 - FileOutputStream is an output stream for writing data to a File or to a FileDescriptor. FileOutputStream is a subclass of OutputStream, which accepts output bytes and sends them to some sink.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › FileOutputStream.html
FileOutputStream (Java Platform SE 8 )
March 16, 2026 - A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other ...
🌐
Jenkov
jenkov.com › tutorials › java-io › fileoutputstream.html
Java FileOutputStream
The Java FileOutputStream in Java IO is used to write binary data to a file in the file system.
🌐
Mkyong
mkyong.com › home › java › how to write to file in java – fileoutputstream
How to write to file in Java - FileOutputStream - Mkyong.com
August 29, 2012 - An updated JDK7 example, using new “try resource close” method to handle file easily. package com.mkyong.io; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class WriteFileExample { public static void main(String[] args) { File file = new File("c:/newfile.txt"); String content = "This is the text content"; try (FileOutputStream fop = new FileOutputStream(file)) { // if file doesn't exists, then create it if (!file.exists()) { file.createNewFile(); } // get the content in bytes byte[] contentInBytes = content.getBytes(); fop.write(contentInBytes); fop.flush(); fop.close(); System.out.println("Done"); } catch (IOException e) { e.printStackTrace(); } } }
🌐
W3Schools
w3schools.com › java › java_fileoutputstream.asp
Java FileOutputStream
The FileOutputStream class works in a similar way, but it writes data as raw bytes. That means you can use it not only for text files, but also for binary files (like images, PDFs, or audio).
🌐
Programiz
programiz.com › java-programming › fileoutputstream
Java FileOutputStream (With Examples)
The FileOutputStream class of the java.io package can be used to write data (in bytes) to the files.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › io › FileOutputStream.html
FileOutputStream (Java Platform SE 6)
FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter. ... Creates an output file stream to write to the file with the specified name.
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › io › FileOutputStream.html
FileOutputStream (Java SE 9 & JDK 9 )
A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other ...
🌐
Javatpoint
javatpoint.com › java-fileoutputstream-class
Java FileOutputStream Class - javatpoint
Java Class Java class is used to write common data into multiple files. In this stream, the data is written into a byte array which can be written to multiple streams later. The holds a copy of data and forwards it to multiple streams.
Top answer
1 of 2
6

FileOutputStream has a constructor that takes a File object.

The following should do what you need it to do:

File f = new File("path/to/my/file");
if(f.createNewFile()) { // may not be necessary
    FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
    ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

    otherMethod(f); // pass the file to your other method
}
2 of 2
0

Note that in addition to the answer of mcfinnigan, you must know that when you use the code:

FileOutputStream fos = new FileOutputStream(f); // create a file output stream around f
ftpClient.retrieveFile("/" + ftpFile.getName(), fos);

Then an empty file will be created on your filesystem on the first line. Then if the 2nd line throws an exception, because no remote file exist for path "/" + ftpFile.getName(), the empty file will still be on your filesystem.

So I've done a little LazyInitOutputStream with Guava to handle that:

public class LazyInitOutputStream extends OutputStream {

  private final Supplier<OutputStream> lazyInitOutputStreamSupplier;

  public LazyInitOutputStream(Supplier<OutputStream> outputStreamSupplier) {
    this.lazyInitOutputStreamSupplier = Suppliers.memoize(outputStreamSupplier);
  }

  @Override
  public void write(int b) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b);
  }

  @Override
  public void write(byte b[]) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b);
  }

  @Override
  public void write(byte b[], int off, int len) throws IOException {
    lazyInitOutputStreamSupplier.get().write(b,off,len);
  }


  public static LazyInitOutputStream lazyFileOutputStream(final File file) {
    return lazyFileOutputStream(file,false);
  }

  public static LazyInitOutputStream lazyFileOutputStream(final File file,final boolean append) {
    return new LazyInitOutputStream(new Supplier<OutputStream>() {
      @Override
      public OutputStream get() {
        try {
          return new FileOutputStream(file,append);
        } catch (FileNotFoundException e) {
          throw Throwables.propagate(e);
        }
      }
    });
  }

I've encoutered this problem while using Spring integration remote.file packages, with the FTP/SFTP file download features. I use it like that to solve this empty file problem:

try ( OutputStream downloadedFileStream = LazyInitOutputStream.lazyFileOutputStream(destinationfilePath.toFile()) ) {
  remoteFileSession.read(source, downloadedFileStream);
}
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › FileOutputStream.html
FileOutputStream (Java SE 11 & JDK 11 )
January 20, 2026 - A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › creating-a-file-using-fileoutputstream
Creating a file using FileOutputStream - GeeksforGeeks
September 11, 2023 - FileOutputStream class belongs to byte stream and stores the data in the form of individual bytes. It can be used to create text files. A file represents storage of data on a second storage media like a hard disk or CD.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › FileOutputStream.html
FileOutputStream (Java SE 21 & JDK 21)
January 20, 2026 - A file output stream is an output stream for writing data to a File or to a FileDescriptor. Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, allow a file to be opened for writing by only one FileOutputStream (or other ...