Search code examples
javafileiorandom-access

splitting file using random file access not working


i want to split a large file in to parts based on size .i used random access file .what i have done was getting the length of files splitting the files and setting file size for each file

File file = new File("/home/hello/test.txt");

if(file.exists()){
        long filesize = file.length();
        int noofparts = 5;
        int perfilesize = (int) (filesize/5);

for(int i=0;i<noofparts;i++){
            try {

                OutputStream os = new FileOutputStream("/home/solomon/test"+i+".txt");
                RandomAccessFile racces = new RandomAccessFile(file, "r");
                racces.seek(seek);
                int read;

                 for(int j=0;j<perfilesize;perfilesize++){
                     os.write(racces.readInt());
                 }

                 os.flush();
                 os.close();





                 seek = seek+perfilesize;


            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

it was splitting but not getting proper output

INPUT

French police were searching the headquarters of Marine Le Pe

.....long text

OUTPUT

rhl eaithqt MnensrgFttaeoa Manloo egontagmsfran doyrmaastaNfaoRe eraaatsih hr Ferr

and the same output is written in the five files


Solution

  • You need to change your loop to

    for(int j=0; j<perfilesize; j++){