Creating a Splitter?

Hi JavaBonds,

I need to create a splitter which will split a file in multiple numbers. So that it can fit in a floppy. Also merge the document whenever it is placed in a different PC.

Can you tell me how do i go about it. Which package i import? I can get such a thing on the net free of cost. I need to put more of learning....that's the motto.

Thank you in advance.

Best Regards,

Tushar

[433 byte] By [tusharm_99] at [2007-9-26 10:30:41]
# 1
java.ioFile, Input/Outputstream etc. etc.
Kayaman at 2007-7-1 22:49:58 > top of Java-index,Archived Forums,Java Programming...
# 2

Here are the basics. Argument and exception handling could be improved, and you'll need to set an appropriate value for the default drectory string. Also, strictly speaking, if you use a really large buffer there isn't any API guarantee that the buffer will be filled even if the file has enough left in it to do that (that is more of an underlying I/O issue than a Java issue; browse through a C Posix book if you want the details). If you want to ensure your output pieces have a particular size (except obviously for the last one) then you'd actually have to check the sizes of your reads to see how much more you wanted to add to an open output file before closing it.

import java.io.*;

//

public class FileSplitter {

//

public static void main(String[] args) {

int floppySize=1400000;

new FileSplitter(args[0],floppySize).split();

}

//

public FileSplitter(String inputFilename, int pieceSize) {

this.inputFilename=inputFilename;

this.pieceSize=pieceSize;

}

//

private String inputFilename;

private int pieceSize;

private String defaultDirPath="your/directory/path";

//

public String outputFilename(int i) {

return inputFilename+"."+i;

}

//

public void split() {

File inputFile=new File(defaultDirPath,inputFilename);

long inputLen=inputFile.length();

FileInputStream inputStream=null;

try {

inputStream=new FileInputStream(inputFile);

FileOutputStream outputStream=null;

byte[] buffer=new byte[pieceSize];

for (int i=0; inputLen > 0; i++) {

try {

int status=inputStream.read(buffer);

if (-1 == status) break;

inputLen-=status;

File outputFile=new File(defaultDirPath,outputFilename(i));

outputStream=new FileOutputStream(outputFile);

outputStream.write(buffer,0,status);

outputStream.close();

} catch (Exception e) {

} finally {

try {

outputStream.close();

} catch (Exception e) {

}

}

}

} catch (Exception e) {

} finally {

try {

inputStream.close();

} catch (Exception e) {

}

}

}

//

}

pinchback at 2007-7-1 22:49:58 > top of Java-index,Archived Forums,Java Programming...