Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- class Assignment22{
- static void println(String str){
- System.out.println(str);
- }
- public static void main(String[] args)throws IOException{
- if(args.length < 2){
- println("Invalid number of arguments");
- println("USAGE: java Assignment22 input.txt out1.txt [ out2.txt ... ]");
- }
- else{
- String in_file = args[0];
- // Opening the input file
- RandomAccessFile in = new RandomAccessFile(in_file, "r");
- // Obtaining size of the file
- long n = in.length();
- // Average size of all files excpet
- long n1 = n / (args.length - 1);
- // Size of the last output file
- long n2 = n - (n1 * (args.length - 2));
- // Splitting the files
- for(int i = 1; i < args.length - 1; i++){
- FileOutputStream out = new FileOutputStream(args[i]);
- // Copying the content to output files except the last
- for(int j = 1; j <= n1; j++){
- out.write(in.read());
- }
- out.close();
- }
- // Copying remaining content to last output file
- FileOutputStream out = new FileOutputStream(args[args.length - 1]);
- // Copying the content to output files except the last
- for(int j = 1; j <= n2; j++){
- out.write(in.read());
- }
- out.close();
- in.close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement