Java FileInputStream getChannel() Method
Example
Get the size of a file with a channel:
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter myWriter = new FileWriter("filename.txt");
myWriter.write("Hello World");
myWriter.close();
FileInputStream input = new FileInputStream("filename.txt");
FileChannel channel = input.getChannel();
System.out.println(channel.size());
input.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 read from 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
❮ FileInputStream Methods