原文摘自:
http://blog.chinaunix.net/uid-26456800-id-3967623.htmlAVA的文件相关类太多了,真扯,谁能记得住啊
Tips:
1.使用RandomAccessFile读取文件,其中seek方法可以按offset移动读取的位置。
2.写byte内容用OutputStream
3.想按byte读内容用InputStreamReader.
4.按行读用BufferedReader,它是一个修饰器,不能直接用一个path new出来。
FileReader filereader = new FileReader("");
BufferedReader bufferedReader=new BufferedReader(filereader);
代码如下
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.util.HashMap;
public class helloworld {
private final static int len = 10000000;
public static void splitfile(String src, String destfolder) throws Exception{
File f = new File(src);
long offset = 0;
int index = 0;
while(offset<f.length()){
String fName = destfolder + "\\" + String.valueOf(index) + ".data";
writefile(src,offset,fName);
offset+=len;
index++;
}
}
public static void writefile(String srcfile, long offset, String destfile) throws Exception{
RandomAccessFile raf = new RandomAccessFile(srcfile,"r");
long bufflen = len;
if(bufflen + offset > raf.length()){
bufflen = raf.length()-offset;
}
byte[] buffer = new byte[(int) bufflen];
raf.seek(offset);
raf.read(buffer);
OutputStream fw = new FileOutputStream(destfile);
fw.write(buffer);
fw.close();
}
/**
* @param args
* @throws Exception
* @throws IOException
*/
public static void main(String[] args) throws IOException, Exception {
// TODO Auto-generated method stub
String path = "c:\\royntest\\wison1@ibm.nsf";
splitfile(path, "c:\\royntest");
}
}
该贴由hui.chen转至本版2015-1-28 15:30:30