java nio读取和写入文件

读取

package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.MessageFormat;

public class TestFileChannel {
    public static void main(String[] args) throws IOException {

        FileInputStream fin = new FileInputStream("D:\\temp\\TestService.cs");

        // 获取通道
        FileChannel fc = fin.getChannel();

        // 创建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        // 读取数据到缓冲区
        fc.read(buffer);

        buffer.flip();

        StringBuffer s=new StringBuffer();
        while (buffer.remaining() > 0) {
            byte b = buffer.get();
            s.append((char)b);
            //System.out.print(((char) b));
        }
        System.out.print(s);

        fin.close();

    }
}

写入

public class Test {
    public static void main(String[] args) throws IOException  {
        File file = new File("data.txt");
        FileOutputStream outputStream = new FileOutputStream(file);
        FileChannel channel = outputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        String string = "java nio";
        buffer.put(string.getBytes());
        buffer.flip();     //此处必须要调用buffer的flip方法
        channel.write(buffer);
        channel.close();
        outputStream.close();
    }
}

原文地址:https://www.cnblogs.com/tiancai/p/8275854.html

时间: 2024-07-31 01:00:42

java nio读取和写入文件的相关文章

Java 一次性读取或写入文件内容

public class IOHelper { public static void copy(Reader in,Writer out) throws IOException { int c = -1; while((c = in.read()) != -1) { out.write(c); } } public static String readFile(File file) throws IOException { if (file != null && file.canRead(

Java 读取、写入文件——解决乱码问题

读取文件流时,经常会遇到乱码的现象,造成乱码的原因当然不可能是一个,这里主要介绍因为文件编码格式而导致的乱码的问题.首先,明确一点,文本文件与二进制文件的概念与差异. 文本文件是基于字符编码的文件,常见的编码有ASCII编码,UNICODE编码.ANSI编码等等.二进制文件是基于值编码的文件,你可以根据具体应用,指定某个值是什么意思(这样一个过程,可以看作是自定义编码.) 因此可以看出文本文件基本上是定长编码的(也有非定长的编码如UTF-8).而二进制文件可看成是变长编码的,因为是值编码嘛,多少

Redis 中文入库成功,读取数据写入文件乱码问题

最近需要用到redis ,但是在编码这个问题上,纠结了很久. 需求 : 每天一个进程将中文文件入库到redis中(不定时更新) ,另外几个进程读取redis中的信息 ,并处理数据结果. 使用的redis模块 : redis-py 问题 : 入库正常,读取数据成功,以GBK编码写入文件出现异常. 通过以下参数连接 redis : client  = redis.StrictRedis(host='localhost', port=6379, db=0, password="***") 从

读取和写入 文件 (NSFIleManger 与 NSFileHandle)

读取和写入 文件 //传递文件路径方法 -(id)initPath:(NSString *)srcPath targetPath:(NSString *)targetPath { self = [super init]; if (self != nil) { _srcPath = [srcPath copy]; _targetPath = [targetPath copy]; } return self; } //开始读文件 -(void)startRead { NSFileManager *f

Java NIO 读取文件、写入文件、读取写入混合

前言 Java NIO(new/inputstream outputstream)使用通道.缓冲来操作流,所以要深刻理解这些概念,尤其是,缓冲中的数据结构(当前位置(position).限制(limit).容量(capacity)),这些知识点要通过写程序慢慢体会. NIO vs  传统IO NIO是面向缓冲.通道的:传统IO面向流 通道是双向的既可以写.也可以读:传统IO只能是单向的 NIO可以设置为异步:传统IO只能是阻塞,同步的 缓冲区结构图 NIO是面向缓冲区的,缓冲区可以理解为一块内存

java nio 读取大文件

package com.yao.bigfile; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class ReadBig { public static final String fileName = "D://log//

Java中读取properties资源文件

一.通过ResourceBundle来读取.properties文件 /** * 通过java.util.resourceBundle来解析properties文件. * @param String path:properties文件的路径 * @param String key: 获取对应key的属性 * @return String:返回对应key的属性,失败时候为空. */ public static String getPropertyByName1(String path,String

【转】MFC中用CFile读取和写入文件2

原文网址:http://blog.sina.com.cn/s/blog_623a7fa40100hh1u.html CFile提供了一些常用的操作函数,如表1-2所示. 表1-2  CFile操作函数 函数 含义 Open 打开文件 Close 关闭文件 Flush 刷新待写的数据 Read 从当前位置读取数据 Write 向当前位置写入数据 GetLength 获取文件的大小 Seek 定位文件指针至指定位置 SeekToBegin 定位文件指针至文件头 SeekToEnd 定位文件指针至文件

C#读取和写入文件

一.读取文件 如果你要读取的文件内容不是很多, 可以使用 File.ReadAllText(FilePath) 或指定编码方式 File.ReadAllText(FilePath, Encoding)的方法. 它们都一次将文本内容全部读完,并返回一个包含全部文本内容的字符串 string str = File.ReadAllText(@"c:\temp\ascii.txt"); //也可以指定编码方式  string str2 = File.ReadAllText(@"c:\