先上代码/**
 * 文件分割器 2009-09-17
 *
 * @author Qiu
 */
public class FileIncise { public static void main(String[] arg0) {
FileIncise demo = new FileIncise(); Long count = 0l;
int currSize = 512000;
String createFolderName = "E:\\test";
String readFileName = "E:\\test\\date.txt"; FileWriter fw;
File tempFile;
char[] str; demo.createFolder(createFolderName);
File file = new File(readFileName);
if (file.exists()) {
count = demo.getCount(file.length(), Long.valueOf(currSize));
System.out.println("文件" + file.getName() + "将被分割为: " + count
+ " 个文件......");
// demo.readFile(file);
int startIndex = 0;
for (int i = 0; i < count; i++) {
startIndex=i*currSize;
str = demo.readFileBySzie(file, currSize,startIndex++);
tempFile=new File(createFolderName+"//"+i+".txt");
//存在则删除重新创建
try {
if(tempFile.createNewFile()){
System.out.println("创建文件"+tempFile.getName()+"成功......");
}else{
System.out.println("文件"+tempFile.getName()+"已存在,将被删除重建......");
if(tempFile.delete()){
tempFile.createNewFile();
System.out.println("重建文件"+tempFile.getName()+"成功......");
}else{
System.out.println("重建文件"+tempFile.getName()+"失败......");
}
}
if(tempFile.exists()){
//FileOutputStream 用于写入诸如图像数据之类的原始字节的流,效率较高
/*OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(tempFile));
osw.write(str,0,str.length);
osw.flush();
osw.close();*/ //FileWriter用于写入字符流效率较高
fw=new FileWriter(tempFile);
System.out.println("正在写入数据......");
fw.write(str,0,str.length);
System.out.println("成功写入数据......");
fw.flush();
fw.close();
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } } else {
System.out.println("要读入的文件不存在......");
} } /**
 * 根据读入的文件大小与要分割的大小返回要生成的文件数
 *
 * @param fileSize
 *            文件大小(byte)
 * @param inciseSize
 *            分割大小(byte)
 * @return 要生成的文件数
 * @author Qiu
 */
public long getCount(Long fileSize, Long inciseSize) {
Long count = fileSize / inciseSize;
Long size = fileSize % inciseSize;
if (size > 0)
count++;
return count;
} /**
 * 根据指定的文件夹名与目录创建一个文件夹
 *
 * @param path
 *            创建路径 d:\\test
 * @return 是否成功
 * @author Qiu
 */
public boolean createFolder(String path) {
File file = new File(path);
boolean flag;
// file.exists();
System.out.println("正在创建文件夹......");
flag = file.mkdir();
if (flag)
System.out.println("创建成功......");
else
System.out.println("创建失败,文件夹已存在......");
return flag;
} /**
 * 按行读取文件内容
 *
 * @param file
 * @return
 * @author Qiu
 */
public void readFile(File file) {
if (file.canRead()) {
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr, 1024);
String line;
line = br.readLine();
while (line != null) {
System.out.println(line);
line = br.readLine();
}
br.close();
fr.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } else {
System.out.println("无法读取文件,文件已损坏或无读取权限......");
}
} /**
 * 按指定大小读取文件并返回内容
 *
 * @param file
 * @param size
 *            指定大小
 * @param startIndex
 *            开始点
 * @return
 * @author Qiu
 */
public char[] readFileBySzie(File file, int size, int startIndex) {
System.out.println("当前读取起始点:"+startIndex+"byte");
if (file.canRead()) {
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
char[] str = new char[size];
br.skip(startIndex);
br.read(str, 0,size);
// System.out.println(str);
return str;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
} } else {
System.out.println("无法读取文件,文件已损坏或无读取权限......");
}
return null;
}
}问题是这样的,date.txt文件的大小是4012K,我想把它按每个文件512k的大小来产生文件!但测试后发现,原本应该产生8个512k大小的文件,实际上产生是4个970K左右的文件,1个570K左右的文件,和3个500K的空文件。查找后发现在读取文件就时候,就产生了问题:最后3次读取到的都是空格。请问应该怎么读取