当前位置:网站首页 > Java教程 > 正文

java通道教程




通道是数据源和Java程序之间的开放连接,用于执行I/O操作。

Channel接口在java.nio.channels包中。

通道接口只声明了两个方法:close()和isOpen()。

ReadableByteChannel用于使用read()方法将数据从数据源读取到字节缓冲区中。 WritableByteChannel用于使用write()方法将数据从字节缓冲区写入数据宿。

ByteChannel能够分别使用read()和write()方法读取和写入字节数据。

ScatteringByteChannel将数据从数据源读取到多个字节缓冲区中。从已知的文件格式或类似的数据源读取数据是有用的,其中在一些固定长度的报头中提供数据,随后是可变长度的主体。

GatheringByteChannel从多个字节缓冲区中写出数据。


要获得一个通道,使用旧的方式使用java.io包中的类使用I/O创建InputStream和OutputStream的对象。

java.nio.channels包中的Channels类是一个实用程序类,它有许多静态方法将流转换为通道,反之亦然。

Channels类还提供了将读写器转换为通道的方法,反之亦然。

例如,如果我们有一个名为myInputStream的输入流对象,我们可以获得一个ReadableByteChannel如下:

ReadableByteChannel rbc = Channels.newChannel(myInputStream); 
InputStream myInputStream = Channels.newInputStream(rbc); 

FileInputStream和FileOutputStream类有一个称为getChannel()的新方法来返回一个FileChannel对象。

FileInputStream fis = new FileInputStream("test1.txt"); FileChannel fcReadOnly = fis.getChannel(); // A read-only channel 
FileOutputStream fos = new FileOutputStream("test1.txt"); FileChannel fcWriteOnly = fos.getChannel(); // A write-only channel 

以下代码为不同种类的文件流获取FileChannel对象:


// read-only mode RandomAccessFile raf1 = new RandomAccessFile("test1.txt", "r"); FileChannel rafReadOnly = raf1.getChannel(); // A read-only channel // read-write mode RandomAccessFile raf2 = new RandomAccessFile("test1.txt", "rw"); FileChannel rafReadWrite = raf2.getChannel(); // A read-write channel 

当我们使用相对read()方法从FileChannel读取时,它的位置增加读取的字节数。

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; public class Main { public static void main(String[] args) { File inputFile = new File("test1.txt"); if (!inputFile.exists()) { System.out.println("The input file " + inputFile.getAbsolutePath() + " does not exist."); System.out.println("Aborted the file reading process."); return; } try (FileChannel fileChannel = new FileInputStream(inputFile).getChannel()) { ByteBuffer buffer = ByteBuffer.allocate(1024); while (fileChannel.read(buffer) > 0) { buffer.flip(); while (buffer.hasRemaining()) { byte b = buffer.get(); System.out.print((char) b); } buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } } } 

上面的代码生成以下结果。

import java.io.File; import java.nio.channels.FileChannel; import java.io.IOException; import java.nio.ByteBuffer; import java.io.FileOutputStream; public class Main { public static void main(String[] args) { File outputFile = new File("test.txt"); try (FileChannel fileChannel = new FileOutputStream(outputFile) .getChannel()) { String text = getText(); byte[] byteData = text.toString().getBytes("UTF-8"); ByteBuffer buffer = ByteBuffer.wrap(byteData); fileChannel.write(buffer); } catch (IOException e1) { e1.printStackTrace(); } } public static String getText() { String lineSeparator = System.getProperty("line.separator"); StringBuilder sb = new StringBuilder(); sb.append("test"); sb.append(lineSeparator); sb.append("test"); sb.append(lineSeparator); sb.append("test"); sb.append(lineSeparator); sb.append("test"); return sb.toString(); } } 

获取源文件和目标文件的FileChannel对象,并对源FileChannel对象调用transferTo()方法或调用目标FileChannel对象上的transferFrom()方法。

以下代码显示如何复制文件。

import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; public class Main { public static void main(String[] args) throws Exception { FileChannel sourceChannel = new FileInputStream("sourceFile").getChannel(); FileChannel sinkChannel = new FileOutputStream("newFile").getChannel(); // Copy source file contents to the sink file sourceChannel.transferTo(0, sourceChannel.size(), sinkChannel); } } 

  • 上一篇: 自学室java教程
  • 下一篇: java电脑视频教程
  • 版权声明


    相关文章:

  • 自学室java教程2025-03-08 22:50:06
  • java特征教程视频2025-03-08 22:50:06
  • java 教程软件2025-03-08 22:50:06
  • java驱动操作教程2025-03-08 22:50:06
  • java底层教程2025-03-08 22:50:06
  • java电脑视频教程2025-03-08 22:50:06
  • appium ios java 教程2025-03-08 22:50:06
  • java项目教程2025-03-08 22:50:06
  • java打卡教程2025-03-08 22:50:06
  • java quickserver教程2025-03-08 22:50:06