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();
        }
    }
}
NIO是Java非阻塞IO的实现,通过使用NIO,可以实现高效的文件批量上传。下面是一个简单的NIO文件批量上传实现的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.InetSocketAddress; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.SocketChannel; import java.util.ArrayList; import java.util.List; public class NIOFileUploader { public static void main(String[] args) throws IOException { List<File> files = new ArrayList<>(); files.add(new File("file1.txt")); files.add(new File("file2.txt")); files.add(new File("file3.txt")); uploadFiles(files); } private static void uploadFiles(List<File> files) throws IOException { SocketChannel socketChannel = SocketChannel.open(); socketChannel.connect(new InetSocketAddress("localhost", 8080)); for (File file : files) { FileInputStream fis = new FileInputStream(file); FileChannel fileChannel = fis.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); while (fileChannel.read(buffer) > 0) { buffer.flip(); socketChannel.write(buffer); buffer.clear(); } fileChannel.close(); fis.close(); } socketChannel.close(); } } ``` 这个示例代码中,我们首先定义了一个文件列表,然后通过一个循环遍历这个列表,依次将每个文件上传到服务器。在上传文件的过程中,我们使用了FileChannel来读取文件内容,并将读取到的数据写入到一个ByteBuffer中。然后,我们再将ByteBuffer中的数据写入到SocketChannel中,从而实现文件的上传。最后,我们关闭了所有的Channel和Stream。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值