FileChannel类的简单用法

本文涉及的产品
函数计算FC,每月15万CU 3个月
简介: 清单一: import java.io.*; import java.nio.*; import java.nio.channels.*; public class GetChannel { private static final int BSIZE = 1024; public static void main(String[] args)thro

清单一:

  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4. public class GetChannel
  5. {
  6. private static final int BSIZE = 1024;
  7. public static void main(String[] args)throws IOException 
  8. {
  9.     FileChannel fc = new FileOutputStream ("data.txt").getChannel();
  10.     fc.write(ByteBuffer.wrap("some txt".getBytes()));//write()    Writes a sequence of bytes to 
  11.     //this channel from the given buffer
  12.     fc.close();
  13.     fc = new RandomAccessFile("data.txt","rw").getChannel();
  14.     fc.position(fc.size());//abstract    FileChannel position(long newPosition) 
  15.                              //Sets this channel's file position. 
  16.     fc.write(ByteBuffer.wrap("some more".getBytes()));
  17.     fc.close();
  18.     fc =new FileInputStream("data.txt").getChannel();
  19.     ByteBuffer bf =    ByteBuffer.allocate(BSIZE);//static ByteBuffer allocate(int capacity) 
  20.                                                     //Allocates a new byte buffer. 
  21.     //一旦调用read()来告知FileChannel向ByteBuffer存储字节,就必须调用缓冲器上的filp(),
  22.     //让它做好别人存储字节的准备(是的,他是有点拙劣,但请记住他是很拙劣的,但却适于获取大速度)
  23.     //
  24.     fc.read(bf);// Reads a sequence of bytes from this channel into the given buffer
  25.     bf.flip();
  26.     while(bf.hasRemaining())
  27.         System.out.print((char)bf.get());
  28. }
  29. }

 

清单二:

  1. //Copying a file using channels and buffers;
  2. import java.io.*;
  3. import java.nio.*;
  4. import java.nio.channels.*;
  5. public class ChannelCopy
  6. {
  7. private static final int BSIZE = 1024;
  8. public static void main(String [] args)throws IOException 
  9. {
  10.    if (args.length!=2)
  11.    {
  12.     System.out.println("argument:sourcefile destfile");
  13.     System.exit(1);
  14.    }
  15.    FileChannel 
  16.         in = new FileInputStream (args[0]).getChannel(),
  17.         out = new FileOutputStream (args[1]).getChannel();
  18.    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
  19.    while (in.read(bb)!=-1)
  20.    {
  21.     bb.flip();
  22.     out.write(bb);
  23.     bb.clear();//prepare for reading;清空缓冲区;
  24.    }
  25. }
  26. }

 

清单三

  1. import java.io.*;
  2. import java.nio.*;
  3. import java.nio.channels.*;
  4. public class TransferTo
  5. {
  6. public static void main(String [] args) throws IOException
  7. {
  8.    if(args.length!=2)
  9.    {
  10.     System.out.println("argument: sourcefile destfile");
  11.     System.exit(1);
  12.    }
  13.    FileChannel
  14.        in = new FileInputStream(args[0]).getChannel(),
  15.        out = new FileOutputStream(args[1]).getChannel();
  16. //abstract   long transferTo(long position, long count, WritableByteChannel target) 
  17. //          Transfers bytes from this channel's file to the given writable byte channel. 
  18.    in.transferTo(0,in.size(),out);
  19.    //or
  20.    //using out
  21. //abstract   long transferFrom(ReadableByteChannel src, long position, long count) 
  22.       //      Transfers bytes into this channel's file from the given readable byte channel. 
  23. // out.transferFrom(in,0,in.size());
  24. }
  25. }
相关实践学习
【文生图】一键部署Stable Diffusion基于函数计算
本实验教你如何在函数计算FC上从零开始部署Stable Diffusion来进行AI绘画创作,开启AIGC盲盒。函数计算提供一定的免费额度供用户使用。本实验答疑钉钉群:29290019867
建立 Serverless 思维
本课程包括: Serverless 应用引擎的概念, 为开发者带来的实际价值, 以及让您了解常见的 Serverless 架构模式
相关文章
|
4月前
|
运维 Java Unix
File类和IO流
File类和IO流
54 0
|
9月前
|
Java
【文件IO】 File类的用法和 InputStream OutputStream 的用法
【文件IO】 File类的用法和 InputStream OutputStream 的用法
|
11月前
|
移动开发 Java Linux
【Java用法】FileWriter的用法 wirte() 方法以及几种重载方法的使用详解
【Java用法】FileWriter的用法 wirte() 方法以及几种重载方法的使用详解
70 0
|
存储 移动开发 Java
IO流以及File类
IO流以及File类
Java:文件io流继承体系文件读取文件写入
Java:文件io流继承体系文件读取文件写入
|
存储 Java
java IO流详细总结(二、字符输入输出流,File文件操作类)
java IO流详细总结(二、字符输入输出流,File文件操作类)
222 0
java IO流详细总结(二、字符输入输出流,File文件操作类)
|
缓存 Java
【Java】文件操作篇(三)字符输入流、字符输出流及常用子类
文章目录 写在前面 1 FileReader与FileWriter 1.1 文件字符流体系概述 1.2 FileReader构造方法与相关方法摘要 1.3 FileWriter构造方法与相关方法摘要 2 案例实操 2.1 FileReader案例 2.2 FileWriter案例 3 处理流 3.1 BufferedReader介绍及使用案例 3.2 BufferedWriter介绍及使用案例 3.3 缓存文件拷贝
【Java】文件操作篇(三)字符输入流、字符输出流及常用子类
Java_IO流07:随机存取文件流(RandomAccessFile 类)
Java_IO流07:随机存取文件流(RandomAccessFile 类)
150 0
Java_IO流07:随机存取文件流(RandomAccessFile 类)
|
存储 Java Unix
09、IO流—File类与IO流(一)
09、IO流—File类与IO流(一)
09、IO流—File类与IO流(一)
|
存储 缓存 Java
09、IO流—File类与IO流(二)
09、IO流—File类与IO流(二)
09、IO流—File类与IO流(二)