import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; import java.nio.channels.Channel; import java.nio.channels.FileChannel; import java.nio.charset.Charset; public class FileChannelMain { private static final Charset charset = Charset.forName("GBK"); private static final int BUFFER_CAPACITY = 1024; public static void main(String[] args) throws IOException, InterruptedException { final String srcfilePath = "D:/tomcat-6.0.26/logs/catalina.2012-04-30.log"; readFile(srcfilePath); final String writeFilePath = "D:/test.txt"; final String[] lines = new String[]{"line1xxssss", "中文测试", "!@#$%^&*()"}; writeFile(writeFilePath, lines, Boolean.TRUE); readFile(writeFilePath); final String targetFilePath = "D:/test-copy.txt"; copyFile1(srcfilePath, targetFilePath); copyFile2(srcfilePath, targetFilePath); } /** * * <br>------------------------------<br> * @param srcfilePath * @param targetPath * @throws IOException */ private static void copyFile2(String srcfilePath, String targetPath) throws IOException { File file = new File(targetPath); if (!file.getParentFile().exists()) { file.mkdirs(); } FileInputStream fileInputStream = new FileInputStream(srcfilePath); FileOutputStream fileOutputStream = new FileOutputStream(file); FileChannel inChannel = fileInputStream.getChannel(); FileChannel outChannel = fileOutputStream.getChannel(); //两者等价 // inChannel.transferTo(0, inChannel.size(), outChannel); outChannel.transferFrom(inChannel, 0, inChannel.size()); close(fileOutputStream); close(fileInputStream); close(inChannel); close(outChannel); } /** * * <br>------------------------------<br> * @param srcfilePath * @param targetPath * @throws IOException */ private static void copyFile1(String srcfilePath, String targetPath) throws IOException { File file = new File(targetPath); if (!file.getParentFile().exists()) { file.mkdirs(); } FileInputStream fileInputStream = new FileInputStream(srcfilePath); FileOutputStream fileOutputStream = new FileOutputStream(file); FileChannel inChannel = fileInputStream.getChannel(); FileChannel outChannel = fileOutputStream.getChannel(); ByteBuffer inBuffer = ByteBuffer.allocate(BUFFER_CAPACITY); while (inChannel.read(inBuffer) != -1) { inBuffer.flip(); outChannel.write(inBuffer); inBuffer.clear(); } close(fileOutputStream); close(fileInputStream); close(inChannel); close(outChannel); } /** * <br>------------------------------<br> * @param writeFilePath * @param lines * @param append * @throws IOException */ private static void writeFile(String writeFilePath, String[] lines, boolean append) throws IOException { File file = new File(writeFilePath); if (!file.getParentFile().exists()) { file.mkdirs(); } FileOutputStream fileOutputStream = new FileOutputStream(file, append); FileChannel fileChannel = fileOutputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY); for (String line : lines) { buffer.put(line.getBytes()); buffer.put("\r\n".getBytes()); buffer.flip(); fileChannel.write(buffer); buffer.clear(); } close(fileOutputStream); close(fileChannel); } /** * <br>------------------------------<br> * @param path * @throws IOException */ private static void readFile(String path) throws IOException { if (isFileNotExists(path)) { throw new FileNotFoundException(); } FileInputStream fileInputStream = new FileInputStream(path); FileChannel fileChanne = fileInputStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(BUFFER_CAPACITY); while (fileChanne.read(buffer) != -1) { buffer.flip(); System.out.println(charset.decode(buffer)); buffer.clear(); } close(fileInputStream); close(fileChanne); } private static boolean isFileNotExists(String path) { File file = new File(path); return !file.exists(); } /** * * <br>------------------------------<br> * @param outputStream */ private static void close(OutputStream outputStream) { if (outputStream == null) return; try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } /** * * <br>------------------------------<br> * @param channel */ private static void close(Channel channel) { if (channel == null ) return; try { channel.close(); } catch (IOException e) { e.printStackTrace(); } } /** * * <br>------------------------------<br> * @param inputStream */ private static void close(InputStream inputStream) { if (inputStream == null) return; try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
NIO - FileChannel
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Java NIO - IO多路复用详解
本文主要对IO多路复用,Ractor模型以及Java NIO对其的支持。
Java IO/NIO/AIO -
【linux】NIO中的FileChannel与mmap
FileChannel是Java NIO库中的一个类,用于对文件进行读写操作。它提供了一种高效的方式来读取、写入和操作文件。
linux nio filechannel mmap 随机读写 -
Java中的NIO详解Day07-FileChannel
FileChannel基本概念开启FileChannel从FileChannel中读取数
java nio 缓存 数据 读取数据 -
nio FileChannel中文乱码问题
最近用nio读取文件时,英文正常,读取中文时会出现乱码,经查可以用Charset类来解决
java经验集锦 JDK java 字符转换 读取文件