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()
}
}
}
}
}