How to split a file in two or more parts.

Hi, I am trying to write a program to split one large file into many small files. (like if file.txt is of 4MB I want to make four files file.txt.p1, file.txt.p2, file.txt.p3 and file.txt.p4 of 1MB each).

Can someone please suggest a proper way of achieving this splitting task?

Thanks in advance.

[315 byte] By [Swing_guya] at [2007-11-27 5:04:17]
# 1

java.io.FileInputStream

java.io.FileOutputStream

$ split --help

Usage: split [OPTION] [INPUT [PREFIX]]

Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default

PREFIX is `x'. With no INPUT, or when INPUT is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.

-a, --suffix-length=Nuse suffixes of length N (default 2)

-b, --bytes=SIZEput SIZE bytes per output file

-C, --line-bytes=SIZEput at most SIZE bytes of lines per output file

-d, --numeric-suffixes use numeric suffixes instead of alphabetic

-l, --lines=NUMBERput NUMBER lines per output file

--verboseprint a diagnostic to standard error just

before each output file is opened

--helpdisplay this help and exit

--version output version information and exit

SIZE may have a multiplier suffix: b for 512, k for 1K, m for 1 Meg.

BIJ001a at 2007-7-12 10:22:34 > top of Java-index,Core,Core APIs...
# 2

Thank you BIJ001 for your effort in helping me out.

But unfortunately, shell command is not what i was asking, probably i did'nt ask it clearly, What exactly I was asking for is this

"Instead of reading data from an input stream and writing it to an output stream, is there any function/chain of functions that can do the task of splitting a file?"

Thank you once more.

Swing_guya at 2007-7-12 10:22:34 > top of Java-index,Core,Core APIs...
# 3

reading from an inputstream and writing to multiple output streams?

No. There is no File.split() method (and reading the File/Stream/IO relevant parts of the API would have told you this), or anything of that sort. You must read and write. Maybe you can find some third-party library that handles this for you, though. Google for one.

masijade.a at 2007-7-12 10:22:34 > top of Java-index,Core,Core APIs...
# 4

I would like to thank you guys BJ100 and masijade. for taking efforts to help me out of this my problem.

masijade. I'd like to tell you that the given task can be achieved by using java.nio.channels.FileChannel class and intelligently providing the parameters to transferTo() method of this class.

Any one having same problem please refer to : http://java.sun.com/developer/JDCTechTips/2002/tt0507.html you'll get some idea about how to use FileChannel class.

Please post other links if any one else knows.

Thank you once more.

Swing_guya at 2007-7-12 10:22:34 > top of Java-index,Core,Core APIs...