Java FileOutputStream getChannel() Method
Example
Write text to a file, then get the size of the file with a channel:
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) throws IOException {
FileOutputStream output = new FileOutputStream("filename.txt");
output.write("Hello World".getBytes());
FileChannel channel = output.getChannel();
System.out.println(channel.size());
output.close();
}
}
Definition and Usage
The getChannel() method returns the FileChannel object of the stream. A channel (from the java.nio package) gives you more ways to work with the file, for example to get its size, or to jump to a position.
The stream and the channel share the same position in the file. When you write to the stream, the position of the channel also moves.
Syntax
public FileChannel getChannel()
Parameter Values
None.
Technical Details
| Returns: | The FileChannel object of the stream. It is the same object each time. |
|---|
Related Pages
Java FileOutputStream Tutorial
❮ FileOutputStream Methods