Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Channel FileChannel to copy Files in java8

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use channel FileChannel to copy files in java8". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use channel FileChannel to copy files in java8.

1. Channel: used for the link between the source node and the target node. In java nio, it is responsible for the transmission of data in the buffer. Channel does not store data in this province. Therefore, it is necessary to cooperate with the buffer for transmission * 2. the main implementation class of the channel * java.nio.channel.Channel API: * |-- FIleChannel* |-- SocketChannel* |-- ServerSocketChannel* |-- DatagramChannel** 3. Get channel * 1.java provides getChannel () method * local IO:* FileInputStream/FileOutputStream* RandomAccessFile** network IO:* Socket* ServerSocket* DatagramSocket* 2 for classes that support channel. NIO.2 in JDK 1.7 provides a static method open () * * 3 for each channel. NewByteChannel () public class TestChannel {/ / 1 of NIO.2 's Files utility class in JDK 1.7. Use the channel to copy @ Test public void test1 () {FileInputStream inputStream= null; FileOutputStream outputStream= null; FileChannel inChannel= null; FileChannel outChannel= null; try {inputStream= new FileInputStream ("1.jpg"); outputStream= new FileOutputStream ("2.jpg"); / / ① to get the channel inChannel= inputStream.getChannel () OutChannel = outputStream.getChannel (); / / ② allocates the buffer ByteBuffer byteBuffer=ByteBuffer.allocate (1024) of the specified size; / / ③ stores the data in the channel into the buffer while (inChannel.read (byteBuffer)! =-1) {byteBuffer.flip () / / switch read data mode / / ④ writes the data in the buffer to the target channel outChannel.write (byteBuffer); byteBuffer.clear ();}} catch (IOException e) {e.printStackTrace ();} finally {if (outChannelined null) {try {outChannel.close () } catch (IOException e) {e.printStackTrace ();}} if {inChannel.close ();} catch (IOException e) {e.printStackTrace () }} if (outputStreamstreams null) {try {outputStream.close ();} catch (IOException e) {e.printStackTrace ();}} if (inputStreamstreams null) {try {inputStream.close () } catch (IOException e) {e.printStackTrace ();} at this point, I believe you have a deeper understanding of "how to use channel FileChannel to copy files in java8". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report