NIO实现文件流插入数据(粘贴功能)

本文提供了一个使用 Java NIO 在指定行和列位置插入文本到文件的示例程序。通过映射文件缓冲区读取源文件,并在目标位置插入新数据,最后将修改后的内容写入新的文件中。
package org1.NIO;

import javafx.stage.Screen;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.Channel;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.util.Scanner;

/**
 * Created by hadoop on 16-9-28.
简单实现功能如下,不知道有没有不这么麻烦的方法
 */
public class PasteDemo {
    public static void main(String[] args) {
        String s ="text.txt";
        int rows = 0;
        int cols = 0;
        Scanner input =new Scanner(System.in);
        System.out.println("请分别输入行号,列号");
        rows =input.nextInt();
        cols =input.nextInt();
        System.out.println("请输入插入的数据:");
        insert(s,rows+1,cols+1,input.next());
    }

    private static void insert(String path, int rows, int cols,String data) {
        File sourceFile =new File(path);
        File distFile =new File("text2.txt");
        FileChannel inChannel;
        FileChannel outChannel;
        try {
            inChannel = new FileInputStream(sourceFile).getChannel();
            MappedByteBuffer mbf =inChannel.map(FileChannel.MapMode.READ_ONLY,0,sourceFile.length());
            CharBuffer  charBuffer = Charset.forName("utf-8").newDecoder().decode(mbf);
            String []context=charBuffer.toString().split("\n");//主意这里的反斜杠!!!!!
            System.out.println("测试分段的数据"+context[rows]);//一行
            String insertLine=context[rows].substring(0,cols)+data+context[rows].substring(cols,context[rows].length());
            System.out.println("测试修改的数据"+insertLine);
            context[rows]=insertLine;

            outChannel =new FileOutputStream(distFile).getChannel();
            ByteBuffer byteBuffer =ByteBuffer.allocate(mbf.capacity()+data.length()*3);
            int contextLen =context.length;
            for (int i =0;i<contextLen;i++){
                byteBuffer.put((context[i]+"\n").getBytes());//按行写入,加换行
            }
            //读取channel,需要置位
            byteBuffer.flip();
            outChannel.write(byteBuffer);
            byteBuffer.clear();//写数据
            System.out.println(byteBuffer);
            System.out.println("position: "+byteBuffer.position()+" limit:"+byteBuffer.limit());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值