package com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class SplitFile
{
/*The method splitFile(File source,int noFile,int lsize) splits the file 'source' into 'noFile' no. of individual files, each file having 'lsize' no. of characters*/
public static File[] splitFile(File source,int noFile,int lsize)
{
File[] fileFragments = new File[noFile];
String[] frgfName = new String[noFile];
try{
String sourceFName = source.getName();
long sourceFSize = source.length();
FileInputStream fis = new FileInputStream(source);
System.out.println(noFile);
if (lsize != 0) {
noFile--;
}
System.out.println(noFile);
sourceFName = sourceFName.substring(0, sourceFName.lastIndexOf("."));
int j=0;
for (int i = 1; i <= noFile; i++) {
frgfName[i-1] = "D:\\"+sourceFName + String.valueOf(i)+".txt";
fileFragments[i-1] = new File(frgfName[i-1]);
FileOutputStream fos = new FileOutputStream(fileFragments[i - 1]);
byte[] data = new byte[lsize];
int count = fis.read(data);
fos.write(data);
fos.close();
String frgFileInfo = new String(frgfName[i-1] + "," + String.valueOf(lsize));
}
if (lsize != 0) {
System.out.println(noFile);
frgfName[noFile] = "D:\\"+sourceFName + String.valueOf(noFile+1)+".txt";
fileFragments[noFile] = new File(frgfName[noFile]);
FileOutputStream fos = new FileOutputStream(fileFragments[noFile]);
byte[] data = new byte[lsize];
int count = fis.read(data);
fos.write(data);
fos.close();
String frgFileInfo = new String(frgfName[noFile] + "," + String.valueOf(lsize));
}
} catch (Exception e) {
System.out.println("Error in Splitting:"+e);
return null;
}
return fileFragments;
}
public static void main(String[] args) {
File keyfile = new File("D:/test.txt"); // the path to the source file
File[] resultFiles=null;
resultFiles=splitFile(keyfile,5,30);
for(File fileObj:resultFiles){
System.out.println(fileObj.getAbsolutePath());
}
}
}
Subscribe to:
Post Comments (Atom)
REFACTORING
What is Refactoring? A software is built initially to serve a purpose, or address a need. But there is always a need for enhancement, fixin...
-
Both SAX and DOM are used to parse the XML document. Both has advantages and disadvantages and can be used in our programming depending on t...
-
If you have ever filled an online form, you are bound to have encountered a CAPTCHA. CAPTCHA stands for Completely Automated Public Turin...
-
This is not my typical technical article, just a little restrospective of an experience at work. I was working with a new member in the t...
1 comment:
Hi Splash Press,
Few points:
(i don't intend to criticize your efforts, but just putting down my thoughts)
1. why are u writing this code when there are many file splitter readily available on net, even for free :)
2. as you have used Stream classes (instead of Reader/Writer) this code should work for binary files as well, right?
3. i couldn't understand ur logic inside "if (lsize != 0)". this condition is at 2 places.
4. generally, either the number of parts is specified (noFile) or the size of each part (lsize). the other value is calculated based on the size of source file. don't know why u r taking noFile and lsize separately.
(there are few more points, but i'll stop here :P :D)
Post a Comment