如何用Java分割大txt文件

Python011

如何用Java分割大txt文件,第1张

import java.io.Fileimport java.io.FileInputStreamimport java.io.FileNotFoundExceptionimport java.io.FileOutputStreamimport java.io.IOExceptionimport java.io.InputStreamimport java.io.OutputStreampublic class FileCutter { /** * *sourceFile:源文件的路径 *targetDirectory:保存文件的目录(例:'C:\\') *prefix:是分割后文件的前缀(例:'2015-09-09') *size:是分隔后单一文件的大小单位是2kb的倍数,size传10,分割后单一文件就是20K。传100,文件就是2M一个。 * **/ public static void cutToMoreFile(String sourceFile, String targetDirectory, String prefix, int size) { //加载源文件 File source = new File(sourceFile) InputStream in = null OutputStream out = null int len = 0 int fileIndex = 1 //设置一次加载的大小 byte[] buffer = new byte[2048] try{//把源文件读到InputStream中in = new FileInputStream(source) //循环while(true){//分割后的文件流 out = new FileOutputStream(targetDirectory + File.separator + prefix + fileIndex++ + ".txt")for(int i = 0i <sizei++) {//如果文件读取完就退回方法。 if((len = in.read(buffer)) != -1) { //写入分割后的文件 out.write(buffer, 0, len) }else { //执行finally内容后,退出方法 return } }} } catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace() } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace() }finally {try {//关系流 in.close()out.close() } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace() } } } }

public static void main(String[] args) {

BufferedReader r = null

BufferedWriter w = null

try {

r = new BufferedReader(new FileReader(new File("e://abc.txt")))

w = new BufferedWriter(new FileWriter(new File("e://abc1.txt")))

int i=2

String buff

while ((buff=r.readLine())!=null) {

w.append(buff).append("\n")

if("".equals(buff)){

w.flush()

w = new BufferedWriter(new FileWriter(new File("e://abc"+i+".txt")))

i++

}

}

w.flush()

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

} finally{

try {

if(r!=null){

r.close()

}

if(w!=null){

r.close()

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}

//什么意思?能描述清楚你想干什么吗?

//切割txt文档?你没说清楚,我就当你是切割文档了!方法挺多的,我就用其中一个好了!

//本方法测试,切割大小为4.5KB的文本文档(皇帝新装),分成5份片段文件!

import java.io.File

import java.io.FileNotFoundException

import java.io.IOException

import java.io.RandomAccessFile

public class CutTest {

static File dir=new File("K:\\IO测试\\随机流测试")//目录路径!

public static void main(String[] args) {

File file=new File(dir,"皇帝新装.txt")//文件!

long size=file.length()//文件大小!

//创建随机流!

RandomAccessFile raf1=null,raf2=null

byte[] bytes=new byte[1024]//缓冲区!

try {

raf1=new RandomAccessFile(file,"r")

for(int i=0,len=0i<=size/1024i++) {

len=raf1.read(bytes)//读入数据!

raf2=new RandomAccessFile(new File(dir,"片段"+(i+1)+".txt"),"rw")

raf2.write(bytes, 0, len)//写出数据!

raf2.close()//关闭!

}

} catch (FileNotFoundException e) {

e.printStackTrace()

}catch(IOException e) {

e.printStackTrace()

}finally {//关流!

if(raf1!=null) {

try {

raf1.close()

} catch (IOException e) {

e.printStackTrace()

}

}

if(raf2!=null) {

try {

raf2.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

}

}