java切割打文本文件

package com.sgcc.controller;

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

public class test {

	
	public static void splitFile(String filePath, int fileCount) throws IOException {
	    FileInputStream fis = new FileInputStream(filePath);
	    FileChannel inputChannel = fis.getChannel();
	    final long fileSize = inputChannel.size();
	    long average = fileSize / fileCount;//平均值
	    long bufferSize = 200; //缓存块大小,自行调整
	    ByteBuffer byteBuffer = ByteBuffer.allocate(Integer.valueOf(bufferSize + "")); // 申请一个缓存区
	    long startPosition = 0; //子文件开始位置
	    long endPosition = average < bufferSize ? 0 : average - bufferSize;//子文件结束位置
	    for (int i = 0; i < fileCount; i++) {
	        if (i + 1 != fileCount) {
	            int read = inputChannel.read(byteBuffer, endPosition);// 读取数据
	            readW:
	            while (read != -1) {
	                byteBuffer.flip();//切换读模式
	                byte[] array = byteBuffer.array();
	                for (int j = 0; j < array.length; j++) {
	                    byte b = array[j];
	                    if (b == 10 || b == 13) { //判断\n\r
	                        endPosition += j;
	                        break readW;
	                    }
	                }
	                endPosition += bufferSize;
	                byteBuffer.clear(); //重置缓存块指针
	                read = inputChannel.read(byteBuffer, endPosition);
	            }
	        }else{
	            endPosition = fileSize; //最后一个文件直接指向文件末尾
	        }

	        FileOutputStream fos = new FileOutputStream(filePath + (i + 1));
	        FileChannel outputChannel = fos.getChannel();
	        inputChannel.transferTo(startPosition, endPosition - startPosition, outputChannel);//通道传输文件数据
	        outputChannel.close();
	        fos.close();
	        startPosition = endPosition + 1;
	        endPosition += average;
	    }
	    inputChannel.close();
	    fis.close();

	}

	public static void main(String[] args) throws Exception {
	    /*Scanner scanner = new Scanner(System.in);
	    scanner.nextLine();*/
	    long startTime = System.currentTimeMillis();
	    splitFile("C:/Users/dell/Desktop/新建文件夹 (3)/osg-uc0010.log",20);
	    long endTime = System.currentTimeMillis();
	    System.out.println("耗费时间: " + (endTime - startTime) + " ms");
	    //scanner.nextLine();
	}
	
	/*public static void main(String[] args) {
		ThreadObj obj = new ThreadObj();
		Thread t1 = new Thread(obj);
		t1.start();
		Thread t2 = new Thread(obj);
		t2.start();
		Thread t3 = new Thread(obj);
		t3.start();
	}*/
	
}

/*class ThreadObj implements Runnable{

	static int i = 0;
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			while(i<100){
				i++;
				System.out.println(Thread.currentThread().getName());	
			}
			i = 0;
			Thread.yield();
		}
	}
	
}*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个Java代码示例,用于对大文本文件进行关键词切割,并将结果输出为各个文件: ```java import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.HashSet; import java.util.Set; public class KeywordSplitter { public static void main(String[] args) { // 定义输入文件路径和输出目录路径 String inputFilePath = "input.txt"; String outputDirectoryPath = "output"; // 定义关键词集合 Set<String> keywords = new HashSet<>(); keywords.add("keyword1"); keywords.add("keyword2"); keywords.add("keyword3"); // 读取输入文件并逐行进行关键词切割 try (BufferedReader reader = new BufferedReader(new FileReader(inputFilePath))) { String line; while ((line = reader.readLine()) != null) { // 对当前行进行关键词切割 Set<String> matchedKeywords = new HashSet<>(); for (String keyword : keywords) { if (line.contains(keyword)) { matchedKeywords.add(keyword); } } // 将匹配到的关键词写入对应的输出文件 for (String matchedKeyword : matchedKeywords) { String outputFilePath = outputDirectoryPath + "/" + matchedKeyword + ".txt"; try (FileWriter writer = new FileWriter(outputFilePath, true)) { writer.write(line + System.lineSeparator()); } catch (IOException e) { e.printStackTrace(); } } } } catch (IOException e) { e.printStackTrace(); } } } ``` 该代码中首先定义了输入文件路径和输出目录路径,以及需要进行关键词切割的关键词集合。然后通过`BufferedReader`逐行读取输入文件,并在每行中查找匹配的关键词。如果匹配到了某个关键词,则将该行写入对应的输出文件中。需要注意的是,如果输出文件已经存在,则将新内容追加到文件末尾。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值