多线程读取单文件

package com.qwx.test;

import java.io.*;

/**
 * @ClassName: ReadFile
 * @Description: java类作用描述
 * @Author: qiaowenxuan
 * @CreateDate: 2021/8/16$ 18:36$
 * @Version: 1.0
 */
public class ReadFile {
    public static void main(String[] args) {
        File file = new File("F://xaa.txt");
        ReaderThread thread1 = new ReaderThread(file);
        ReaderThread thread2 = new ReaderThread(file);

        new Thread(thread1,"thread1").start();
        new Thread(thread2,"thread2").start();
    }
}

class Reader {
    public static void read(File file) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);
        InputStreamReader reader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line = null;
        while (null != (line = bufferedReader.readLine())) {
            System.out.println(line);
        }
        bufferedReader.close();
    }
}

class ReaderThread implements Runnable {

    private File file;
    public ReaderThread(File file) {
        this.file = file;
    }

    @Override
    public void run() {
        try {
            Reader.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

多线程读取文件列表

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.Jedis;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class FavFileUtil {
    public static List getFileList(String filePath) {
        List fileList = new ArrayList();
        File file = new File(filePath);
        if (!file.isDirectory()) {
            System.out.println("文件【" + file.getName() + "】:" + file.getAbsolutePath());
            fileList.add(file);
        } else {
            System.out.println("文件夹【" + file.getName() + "】:" + file.getAbsolutePath());
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (!files[i].isDirectory()) {
                    System.out.println("  文件【" + files[i].getName() + "】:");
                    fileList.add(files[i]);
                } else if (files[i].isDirectory()) {
                    getFileList(files[i].getAbsolutePath());
                }
            }
        }
        return fileList;
    }

    public static List subFiles(List list, int startIndex, int endIndex) {
        if (endIndex > list.size()) {
            return list.subList(startIndex, list.size());
        }
        return list.subList(startIndex, endIndex);
    }

    static int i = 0;
    public static void readFile(File file) throws IOException {
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        //读取文件
        FileInputStream is = new FileInputStream(file);

        //获取文件的字符流
        InputStreamReader isr = new InputStreamReader(is);

        //缓冲
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        while ((line = br.readLine()) != null) {
            String[] split = line.split("----");
            String key = split[0];
            String val = split[1];
            //if (null == key) key = "";
            //if(null == val) val ="";
            jedis.set(key,val);
            System.out.println(i);
            System.out.println(Thread.currentThread().getName() + ":::" + line);
            System.out.println(Thread.currentThread().getName() + ":::" + key+"----"+val);
            i++;
        }
        br.close();
    }

}
import java.io.File;
import java.io.IOException;
import java.util.List;

public class FavThreadUtil implements Runnable {
    private List<File> fileList;
    public FavThreadUtil(List fileList) {
        this.fileList = fileList;
    }
    @Override
    public void run() {
        for(File file : fileList){
            try {
                FavFileUtil.readFile(file);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐